Mockito unit test - test calculator method with argument

Multi tool use
I'm trying to test my method with argument with Mockito.
In my opinion I didn't understand the rules of the mockito operation.
I got this from Android Studio:
Comparison Failure:
Expected :view.showResultCalculation(4);
Actual :view.showResultCalculation(0);
My test:
public class MainActivityTest {
@Mock
Calculator calculator;
@Mock
IMainActivity.View view;
private MainActivityPresenter presenter;
@Before
public void setUp() {
presenter = new MainActivityPresenter(calculator);
presenter.setView(view);
}
@Test
public void should_addNumber_when_buttonCLicked(){
presenter.doCalculate(2,2);
verify(view).showResultCalculation(4);
}
}
My calculator have only one method:
public class Calculator {
public int addition(int a, int b){
return a+b;
}
}
My presenter have this method:
@Override
public void doCalculate(int a, int b) {
getView().showResultCalculation(calculator.addition(a,b));
}
and my View - main activity - have this method:
@Override
public void showResultCalculation(int result) {
this.result.setText(String.valueOf(result));
}
In real using, this calculator works, and we got 2+2=4, but test give result 0.
What I should fix here ?
java

add a comment |
I'm trying to test my method with argument with Mockito.
In my opinion I didn't understand the rules of the mockito operation.
I got this from Android Studio:
Comparison Failure:
Expected :view.showResultCalculation(4);
Actual :view.showResultCalculation(0);
My test:
public class MainActivityTest {
@Mock
Calculator calculator;
@Mock
IMainActivity.View view;
private MainActivityPresenter presenter;
@Before
public void setUp() {
presenter = new MainActivityPresenter(calculator);
presenter.setView(view);
}
@Test
public void should_addNumber_when_buttonCLicked(){
presenter.doCalculate(2,2);
verify(view).showResultCalculation(4);
}
}
My calculator have only one method:
public class Calculator {
public int addition(int a, int b){
return a+b;
}
}
My presenter have this method:
@Override
public void doCalculate(int a, int b) {
getView().showResultCalculation(calculator.addition(a,b));
}
and my View - main activity - have this method:
@Override
public void showResultCalculation(int result) {
this.result.setText(String.valueOf(result));
}
In real using, this calculator works, and we got 2+2=4, but test give result 0.
What I should fix here ?
java

Looks like you're mocking the calculator.@Mock Calculator
. But I don't see that you've mocked theaddition
method. Is it possible that your mock calculator is returning 0 for any input?
– mkasberg
Dec 30 '18 at 20:56
yes, this calculator in test always return 0, but only in test
– purcha
Dec 30 '18 at 21:26
1
why are you event bothering to mock theCalculator
class for this? Surely you would presently dowhen(calculator.addition(eq(2), eq(2))).thenReturn(4);
or something similar?
– Mark Keen
Dec 30 '18 at 22:22
that's a bad design , MVP clean?
– notTdar
Dec 31 '18 at 13:20
So you've answered your own question then. If the calculator always returns 0 in tests (because it's mocked), That is why you're gettingActual :view.showResultCalculation(0);
. The view is called with the result from the mocked calculator. There are a number of simple solutions. The simplest fix, as Mark said, is to not mock the calculator in this test. (Use a real instance instead.)
– mkasberg
Jan 1 at 18:24
add a comment |
I'm trying to test my method with argument with Mockito.
In my opinion I didn't understand the rules of the mockito operation.
I got this from Android Studio:
Comparison Failure:
Expected :view.showResultCalculation(4);
Actual :view.showResultCalculation(0);
My test:
public class MainActivityTest {
@Mock
Calculator calculator;
@Mock
IMainActivity.View view;
private MainActivityPresenter presenter;
@Before
public void setUp() {
presenter = new MainActivityPresenter(calculator);
presenter.setView(view);
}
@Test
public void should_addNumber_when_buttonCLicked(){
presenter.doCalculate(2,2);
verify(view).showResultCalculation(4);
}
}
My calculator have only one method:
public class Calculator {
public int addition(int a, int b){
return a+b;
}
}
My presenter have this method:
@Override
public void doCalculate(int a, int b) {
getView().showResultCalculation(calculator.addition(a,b));
}
and my View - main activity - have this method:
@Override
public void showResultCalculation(int result) {
this.result.setText(String.valueOf(result));
}
In real using, this calculator works, and we got 2+2=4, but test give result 0.
What I should fix here ?
java

I'm trying to test my method with argument with Mockito.
In my opinion I didn't understand the rules of the mockito operation.
I got this from Android Studio:
Comparison Failure:
Expected :view.showResultCalculation(4);
Actual :view.showResultCalculation(0);
My test:
public class MainActivityTest {
@Mock
Calculator calculator;
@Mock
IMainActivity.View view;
private MainActivityPresenter presenter;
@Before
public void setUp() {
presenter = new MainActivityPresenter(calculator);
presenter.setView(view);
}
@Test
public void should_addNumber_when_buttonCLicked(){
presenter.doCalculate(2,2);
verify(view).showResultCalculation(4);
}
}
My calculator have only one method:
public class Calculator {
public int addition(int a, int b){
return a+b;
}
}
My presenter have this method:
@Override
public void doCalculate(int a, int b) {
getView().showResultCalculation(calculator.addition(a,b));
}
and my View - main activity - have this method:
@Override
public void showResultCalculation(int result) {
this.result.setText(String.valueOf(result));
}
In real using, this calculator works, and we got 2+2=4, but test give result 0.
What I should fix here ?
java

java

asked Dec 30 '18 at 20:36
purchapurcha
707
707
Looks like you're mocking the calculator.@Mock Calculator
. But I don't see that you've mocked theaddition
method. Is it possible that your mock calculator is returning 0 for any input?
– mkasberg
Dec 30 '18 at 20:56
yes, this calculator in test always return 0, but only in test
– purcha
Dec 30 '18 at 21:26
1
why are you event bothering to mock theCalculator
class for this? Surely you would presently dowhen(calculator.addition(eq(2), eq(2))).thenReturn(4);
or something similar?
– Mark Keen
Dec 30 '18 at 22:22
that's a bad design , MVP clean?
– notTdar
Dec 31 '18 at 13:20
So you've answered your own question then. If the calculator always returns 0 in tests (because it's mocked), That is why you're gettingActual :view.showResultCalculation(0);
. The view is called with the result from the mocked calculator. There are a number of simple solutions. The simplest fix, as Mark said, is to not mock the calculator in this test. (Use a real instance instead.)
– mkasberg
Jan 1 at 18:24
add a comment |
Looks like you're mocking the calculator.@Mock Calculator
. But I don't see that you've mocked theaddition
method. Is it possible that your mock calculator is returning 0 for any input?
– mkasberg
Dec 30 '18 at 20:56
yes, this calculator in test always return 0, but only in test
– purcha
Dec 30 '18 at 21:26
1
why are you event bothering to mock theCalculator
class for this? Surely you would presently dowhen(calculator.addition(eq(2), eq(2))).thenReturn(4);
or something similar?
– Mark Keen
Dec 30 '18 at 22:22
that's a bad design , MVP clean?
– notTdar
Dec 31 '18 at 13:20
So you've answered your own question then. If the calculator always returns 0 in tests (because it's mocked), That is why you're gettingActual :view.showResultCalculation(0);
. The view is called with the result from the mocked calculator. There are a number of simple solutions. The simplest fix, as Mark said, is to not mock the calculator in this test. (Use a real instance instead.)
– mkasberg
Jan 1 at 18:24
Looks like you're mocking the calculator.
@Mock Calculator
. But I don't see that you've mocked the addition
method. Is it possible that your mock calculator is returning 0 for any input?– mkasberg
Dec 30 '18 at 20:56
Looks like you're mocking the calculator.
@Mock Calculator
. But I don't see that you've mocked the addition
method. Is it possible that your mock calculator is returning 0 for any input?– mkasberg
Dec 30 '18 at 20:56
yes, this calculator in test always return 0, but only in test
– purcha
Dec 30 '18 at 21:26
yes, this calculator in test always return 0, but only in test
– purcha
Dec 30 '18 at 21:26
1
1
why are you event bothering to mock the
Calculator
class for this? Surely you would presently do when(calculator.addition(eq(2), eq(2))).thenReturn(4);
or something similar?– Mark Keen
Dec 30 '18 at 22:22
why are you event bothering to mock the
Calculator
class for this? Surely you would presently do when(calculator.addition(eq(2), eq(2))).thenReturn(4);
or something similar?– Mark Keen
Dec 30 '18 at 22:22
that's a bad design , MVP clean?
– notTdar
Dec 31 '18 at 13:20
that's a bad design , MVP clean?
– notTdar
Dec 31 '18 at 13:20
So you've answered your own question then. If the calculator always returns 0 in tests (because it's mocked), That is why you're getting
Actual :view.showResultCalculation(0);
. The view is called with the result from the mocked calculator. There are a number of simple solutions. The simplest fix, as Mark said, is to not mock the calculator in this test. (Use a real instance instead.)– mkasberg
Jan 1 at 18:24
So you've answered your own question then. If the calculator always returns 0 in tests (because it's mocked), That is why you're getting
Actual :view.showResultCalculation(0);
. The view is called with the result from the mocked calculator. There are a number of simple solutions. The simplest fix, as Mark said, is to not mock the calculator in this test. (Use a real instance instead.)– mkasberg
Jan 1 at 18:24
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981231%2fmockito-unit-test-test-calculator-method-with-argument%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53981231%2fmockito-unit-test-test-calculator-method-with-argument%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
OgI4ly5daLrEDdX5ks suVx1f8OhOSQp12Srjjot 2,mi51KT
Looks like you're mocking the calculator.
@Mock Calculator
. But I don't see that you've mocked theaddition
method. Is it possible that your mock calculator is returning 0 for any input?– mkasberg
Dec 30 '18 at 20:56
yes, this calculator in test always return 0, but only in test
– purcha
Dec 30 '18 at 21:26
1
why are you event bothering to mock the
Calculator
class for this? Surely you would presently dowhen(calculator.addition(eq(2), eq(2))).thenReturn(4);
or something similar?– Mark Keen
Dec 30 '18 at 22:22
that's a bad design , MVP clean?
– notTdar
Dec 31 '18 at 13:20
So you've answered your own question then. If the calculator always returns 0 in tests (because it's mocked), That is why you're getting
Actual :view.showResultCalculation(0);
. The view is called with the result from the mocked calculator. There are a number of simple solutions. The simplest fix, as Mark said, is to not mock the calculator in this test. (Use a real instance instead.)– mkasberg
Jan 1 at 18:24