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)
}
}
-
Why are you trying to use Mockito.when() on MyClass in the first place? You can only use "when" on mocks.StrongPoint– StrongPoint2022年08月23日 14:05:22 +00:00Commented 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.dredbound– dredbound2022年08月23日 14:07:03 +00:00Commented 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).StrongPoint– StrongPoint2022年08月23日 14:09:41 +00:00Commented 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"?dredbound– dredbound2022年08月23日 14:13:47 +00:00Commented Aug 23, 2022 at 14:13
-
ie when(otherMethod("hostname")).thenReturn(something)dredbound– dredbound2022年08月23日 14:16:03 +00:00Commented Aug 23, 2022 at 14:16
1 Answer 1
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));
}
}
4 Comments
Error: reference call is ambiguous when(myMethodRevisionCall.call(any())).thenReturn(otherMethod); both method call(someObject) in MyMethodRevisionCall and method call(Envelope) in MyMethodRevisionCall matchcom.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()Explore related questions
See similar questions with these tags.