mockito Object return zero

Created on 9 Nov 2016  Â·  3Comments  Â·  Source: mockito/mockito

Hey,

I test project with mockit-all.1.9.5.jar.
I expect result is 4, but the result is 0. When debugging that I found the mock object return is 0, so why ?

public class CalculatorServiceSpringImpl implements CalculatorService {
    private AddService addService = new AddServiceSpringImpl();
    private SubtractService subtractService = new SubtractServiceSpringImpl();

    @Override
    public int result(int initNumber) {
        int result = 1;
           //+1
        result = addService.addOne(initNumber);
          //-1
        result = subtractService.subtractOne(result);
        return result;
    }

}

@Test
public void testResult() {
    CalculatorService calculatorService = new CalculatorServiceSpringImpl();
    AddService addServiceMock = mock(AddServiceSpringImpl.class);
     SubtractService subtractServiceMock = mock(SubtractServiceSpringImpl.class);
     when(addServiceMock.addOne(anyInt())).thenReturn(5);
     when(subtractServiceMock.subtractOne(3)).thenReturn(10);
    int result= calculatorService.result(1);
     assertEquals(4 ,result);
}
awaiting response question

All 3 comments

You must pass the mocked instances of AddServiceImpl and SubtractServiceImpl to the CalculatorServiceImpl. Currently you use the instances you create in the field declaration.

This does the trick:

public CalculatorServiceSpringImpl(AddService  a, SubtractService  s){
 addService= a;
 subtractService = s;
}
AddService a= mock(AddServiceSpringImpl.class);
SubtractService s= mock(SubtractServiceSpringImpl.class);
CalculatorService calculatorService = new CalculatorServiceSpringImpl(a, s);

happy mocking

I'm confusing is the above code to invoke the mock service,but the following code is invoke the realMethod .

@ChristianSchwarz

When I given 1 to calculatorService.result(),It's return 1,invoice the realMethod.
When I given 3 to calculatorService.result(), It's return 0, invoice the mockMethod.

@Test
public void testResult() {

    CalculatorService calculatorService = new CalculatorServiceSpringImpl();
    AddService addServiceMock = mock(AddServiceSpringImpl.class);
     SubtractService subtractServiceMock = mock(SubtractServiceSpringImpl.class);

     when(addServiceMock.addOne(3)).thenReturn(5);
     when(subtractServiceMock.subtractOne(3)).thenReturn(10);        
    int result= calculatorService.result(1);
     Assert.assertEquals(1 ,result);
}

Once again thank you for your answer.

The code snippet shows that the two mocks you defined are not passed into
the calculatorService. You should create the mocks and then supply them via
the constructor or a setter. Only then the mocks are actually used and the
stubbed methods will be invoked.

On Thu, 10 Nov 2016, 09:51 yujishe, [email protected] wrote:

I'm confusing is the above code to invoke the mock service,but the
following code is invoke the realMethod .
When I given 1 to calculatorService.result(),It's return 1,invoice the
realMethod.
When I given 3 to calculatorService.result(), It's return 0, invoice the
mockMethod.

@Test https://github.com/Test
public void testResult() {

CalculatorService calculatorService = new CalculatorServiceSpringImpl();
AddService addServiceMock = mock(AddServiceSpringImpl.class);
 SubtractService subtractServiceMock = mock(SubtractServiceSpringImpl.

class);

 when(addServiceMock.addOne(3)).thenReturn(5);
 when(subtractServiceMock.subtractOne(3)).thenReturn(10);
int result= calculatorService.result(1);
 Assert.assertEquals(1 ,result);

}

Once again thank you for your answer.

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/mockito/mockito/issues/749#issuecomment-259634238,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFrDbyJqU-rtitbP6wyeCS7O_wRrhB4Eks5q8tsNgaJpZM4KtT02
.

Was this page helpful?
0 / 5 - 0 ratings