Mockito stubbing returns null

I use mockito as a mocking structure. I have a scenerio here, mine when (abc.method ()). ThenReturn (value) does not return a value, instead returns null.

This is what my class and test look like.

public class foo(){  
 public boolean method(String userName) throws Exception {
    ClassA request = new ClassA();
    request.setAbc(userName);       
    ClassB response = new ClassB();
    try {
        response = stub.callingmethod(request);
    } catch (Exception e) {
    }

    boolean returnVal = response.isXXX();
    return returnVal;
}  

Now the next test is

@Test
public void testmethod() throws Exception{
    //arrange
    String userName = "UserName";
    ClassA request = new ClassA();
    ClassB response = new ClassB();
    response.setXXX(true);
    when(stub.callingmethod(request)).thenReturn(response);
    //act
    boolean result = fooinstance.lockLogin(userName);

    //assert
    assertTrue(result);
}

stub mocks using mockito using @Mock. The test throws a NullPointerException in the foo class near boolean retrunVal = response.isXXX ();

+3
source share
2 answers

argument matching for stub.callingmethod (request). thenReturn (response) compares for reference equality. You want more free matches, as I think:

stub.callingmethod(isA(ClassA.class)).thenReturn(response);
+6
source

Make sure yours ClassAimplements its own equalsand that it is correctly implemented.

0
source

All Articles