Reputation: 99
I have below method in my code
public <T> T doHttpPost(String url, String data, BiFunction<String, Integer, T> responseHandler, Consumer<Exception> exceptionHandler) {
...
}
The above method is used as below
return APIClient.doHttpPost(url, EmptyString, null, null);
Even when trying to pass methods instead of nulls to the 2 arguments above, i am still getting the error
During writing my unit test methods, I am using the mock which is setup as below,
doAnswer(ans -> true).when(apiClient).doHttpPost(anyString(), anyString(), any(BiFunction.class), any(Consumer.class));
doAnswer(ans -> true).when(apiClient).doHttpPost(anyString(), any(HttpEntity.class), any(BiFunction.class), any(Consumer.class));
However, the null pointer exception still occurred, hence I tried to change the above as below in the mock setup and still getting the null pointer exception
when(apiClient.doHttpPost(anyString(), anyString(), any(BiFunction.class), any(Consumer.class))).thenReturn(true);
when(apiClient.doHttpPost(anyString(), any(HttpEntity.class), any(BiFunction.class), any(Consumer.class))).thenReturn(true);
The mock setup for other regular methods are working fine, they have no generic implementations.
When I checked the method call in the watch window in IntelliJ IDE, i found that the method returns null instead of true which is configured in the mock. If there is a way to find why these 2 methods in the mock are not setup correctly, it will be easy for fixing
EDIT Based on the suggestion by @Marco, I have tried the following
doAnswer(ans -> true).when(apiClient).doHttpPost(anyString(), anyString(), any(), any());
when using the below invocation methods,
return jenkinsAPIClient.doHttpPost(url, EmptyString, null, null);
The test fails with the below error
Argument(s) are different! Wanted:
APIClient#0 bean.doHttpPost(
<any string>,
<any string>,
<any java.util.function.BiFunction>,
<any java.util.function.Consumer>
);
testcasename(ServiceTests.java:435)
Actual invocations have different arguments:
APIClient#0 bean.doHttpPost(
"nulljob/CL217R/job/One/doRename?newName=aYbf8y8FRirlgXQoz7ySNhQqNp870xY0tZ9O6sSSBlbfgktoM7zb3UNdMNbKMQM9amMLio9KMzLtoOmnuj9z0wvA72h1L8oSGbLVqQQbmso4PTXMhFMfc14we03",
"",
null,
null
);
In case, I use the below method call,
return jenkinsAPIClient.doHttpPost(url, EmptyString, jenkinsAPIClient.successStatusCheck, jenkinsAPIClient.GenericExceptionHandler);
The test case fails with the below error
Argument(s) are different! Wanted:
APIClient#0 bean.doHttpPost(
<any string>,
<any string>,
<any java.util.function.BiFunction>,
<any java.util.function.Consumer>
);
testcasename(ServiceTests.java:435)
Actual invocations have different arguments:
APIClient#0 bean.doHttpPost(
"nulljob/CL217R/job/One/doRename?newName=aYbf8y8FRirlgXQoz7ySNhQqNp870xY0tZ9O6sSSBlbfgktoM7zb3UNdMNbKMQM9amMLio9KMzLtoOmnuj9z0wvA72h1L8oSGbLVqQQbmso4PTXMhFMfc14we03",
"",
null,
APIClient$$Lambda$128/2000563893@360bc645
);
Upvotes: 0
Views: 662
Reputation: 587
When you are using null
as a agrs in the method that isn't a String or object is a simple null for that reason you need to mock the call in the following way:
doAnswer(ans -> true).when(apiClient).doHttpPost(anyString(), anyString(), any(), any());
With only Mockito.any() should work because works with anything including nulls.
Upvotes: 1