0

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)
 }
}
asked Aug 23, 2022 at 13:39
7
  • Why are you trying to use Mockito.when() on MyClass in the first place? You can only use "when" on mocks. Commented Aug 23, 2022 at 14:05
  • I'm new with java, mocks so I may be doing it wrong. I was trying to mock "otherMethod" inside of myClass. Commented Aug 23, 2022 at 14:07
  • You should only mock responses on dependencies of the class you want to test. So in your case you'll want to do when(myClient.newMyMethodRevisionCall()).thenReturn(something). Commented Aug 23, 2022 at 14:09
  • But I'm still trying to test the method "myMethod" which calls the method "otherMethod" so shouldn't I mock "otherMethod"? Commented Aug 23, 2022 at 14:13
  • ie when(otherMethod("hostname")).thenReturn(something) Commented Aug 23, 2022 at 14:16

1 Answer 1

2

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));
 }
}
answered Aug 23, 2022 at 14:50
Sign up to request clarification or add additional context in comments.

4 Comments

Tried this, it seems to give me the following error: Error: reference call is ambiguous when(myMethodRevisionCall.call(any())).thenReturn(otherMethod); both method call(someObject) in MyMethodRevisionCall and method call(Envelope) in MyMethodRevisionCall match
Use any(SomeObject.class) then.
I'm now getting this error: com.path.to.MyClassTest > test_myMethod() FAILED java.lang.NullPointerException at com.path.to.MyClass.otherMethod(MyClass.java:66) at com.path.to.MyClass.myMethod(MyClass.java:75) at com.path.to.MyClassTest.test_myMethod(MyClassTest.java:65) It looks like myClient is evaluating to null inside of otherMethod and returns the Null Point Exception again when it gets to this: myClient.newMyMethodRevisionCall()
Then MyClient is not properly mocked in your test. Look at the setup Method in my Example. I first mock MyClient and then pass the mock into MyClass.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.