Junit 调用mockito mock函数时出现NullPointerException

Junit 调用mockito mock函数时出现NullPointerException,junit,java-8,mockito,Junit,Java 8,Mockito,我的代码中有以下方法 public <T> T doHttpPost(String url, String data, BiFunction<String, Integer, T> responseHandler, Consumer<Exception> exceptionHandler) { ... } 即使尝试将方法而不是null传递给上面的2个参数,我仍然会遇到错误 在编写单元测试方法的过程中,我使用了模拟,其设置如下: doAnswer(ans -

我的代码中有以下方法

 public <T> T doHttpPost(String url, String data, BiFunction<String, Integer, T> responseHandler, Consumer<Exception> exceptionHandler) {
...
}
即使尝试将方法而不是null传递给上面的2个参数,我仍然会遇到错误

在编写单元测试方法的过程中,我使用了模拟,其设置如下:

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));
但是,空指针异常仍然发生,因此我尝试在模拟设置中更改上述内容,但仍然得到空指针异常

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);
其他常规方法的模拟设置工作正常,没有通用实现

当我在IntelliJ IDE的watch窗口中检查方法调用时,我发现该方法返回null而不是模拟中配置的true。如果有办法找到模拟中这两个方法设置不正确的原因,那么就很容易修复

编辑 根据@Marco的建议,我尝试了以下方法

doAnswer(ans -> true).when(apiClient).doHttpPost(anyString(), anyString(), any(), any());
使用以下调用方法时

return jenkinsAPIClient.doHttpPost(url, EmptyString, null, null);
测试失败,出现以下错误

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
);
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
);
测试用例失败,出现以下错误

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
);
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
);
参数不同!通缉:
APIClient#0 bean.doHttpPost(
,
,
,
);
testcasename(ServiceTests.java:435)
实际调用有不同的参数:
APIClient#0 bean.doHttpPost(
“nulljob/CL217R/job/One/doRename?新名称=AYBF8Y8FRIRLGXQOZ7YSNHQNP870XY0TZ9O6SSSBLBFGKTOM7ZB3UNDMNBKMQM9AMMLIO9KMZLTOOMNUJ9Z0WVA72H1L8OSGBLVQQBMSO4PTHFMFC14WE03”,
"",
无效的
APIClient$$Lambda$128/2000563893@360bc645
);

当您使用
null
作为agrs时,方法中的非字符串或对象是简单的null,因此您需要通过以下方式模拟调用:

doAnswer(ans -> true).when(apiClient).doHttpPost(anyString(), anyString(), any(), any());

只使用Mockito.any()应该可以工作,因为它可以处理包括空值在内的任何内容。

欢迎使用堆栈溢出。请学习如何使用堆栈溢出,并阅读如何提高问题的质量。然后你的问题包括你的源代码,它可以被其他人编译和测试。谢谢你的回答,我已经用试用和错误消息更新了帖子,你能检查相同的,并建议修改吗。