Home > Mobile >  Java Unittest with Mockito NullPointer Exception Error
Java Unittest with Mockito NullPointer Exception Error

Time:08-25

I'm trying to write a unittest (test_myMethod) for one of my methods (myMethod) but I can't seem to get it to work. I get a NullPointer exception error with the code below. In my test it seems that the line myClass.otherMethod("hostName") in the unittest evaluates to Null so it can't do .getOSRevision(). Anyone know how I can get my unittest to pass?

MyClass.java

public class MyClass {
    public String myMethod(final String hostname) {
        return otherMethod(hostname).getOSRevision();
    }

    public OtherMethod otherMethod(final String hostname) {
        OtherMethod response = myClient.newMyMethodRevisionCall().call(
            someObject.builder()
                .withHostName(hostname)
                .build()
        );
        return response; 
    }
   
}

MyClassTest.java

public class MyClassTest {
    @Mock
    private MyClient myClient;

    @Mock
    private MyMethodRevisionCall myMethodRevisionCall;

    @Mock
    private OtherMethod otherMethod;
    
    private MyClass myClass;

    @BeforeEach
        void setUp() {
            MockitoAnnotations.initMocks(this);
            when(myClient.newMyMethodRevisionCall()).thenReturn(myMethodRevisionCall);
            myClass = new MyClass(myClient);
    }

    // My failing unittest attempt
    @Test
    public void test_myMethod() {
        when(myClass.otherMethod("hostName")
        .getOSRevision())    //java.lang.NullPointerException happens on this line.
        .thenReturn("TestString");

        final String result = myClass.myMethod("TestString");
        verify(myMethodRevisionCall, times(1)
    }
}

CodePudding user response:

You cannot use Mockito.when() on classes that are not mocked by Mockito. MyClass is not a mock. Instead you create an actual instance of it. Therefore, you need to actually run the code and not mock it. However, all dependencies of your class under test (MyClient in your case) you can mock.

This is a working example:

public class MyClass {
    private MyClient myClient;

    public MyClass(MyClient myClient) {
        this.myClient = myClient;
    }

    public String myMethod(final String hostname) {
        return otherMethod(hostname).getOSRevision();
    }

    public OtherMethod otherMethod(final String hostname) {
        return myClient.newMyMethodRevisionCall().call(new SomeObject(hostname));
    }
}
class MyClassTest {
    private MyClient myClient;
    private MyClass myClass;

    @BeforeEach
    void setUp() {
        myClient = mock(MyClient.class);
        myClass = new MyClass(myClient);
    }

    @Test
    public void test_myMethod() {
        MyMethodRevisionCall myMethodRevisionCall = mock(MyMethodRevisionCall.class);
        OtherMethod otherMethod = mock(OtherMethod.class);
        when(myClient.newMyMethodRevisionCall()).thenReturn(myMethodRevisionCall);
        when(myMethodRevisionCall.call(any())).thenReturn(otherMethod);
        when(otherMethod.getOSRevision()).thenReturn("revision");

        final String result = myClass.myMethod("TestString");

        assertEquals("revision", result);
        verify(myMethodRevisionCall, times(1));
    }
}
  • Related