JAVA:传递给when()的JUNIT参数不是模拟
我正在尝试对以下函数运行单元测试
@Autowired
private EmailServiceConfigProperties emailServiceConfigProperties;
@Autowired
private HttpHandler httpHandler;
public TransactionalEmailResponse sendPostRequestForTransactionalEmail(String endpoint, TransactionalEmail emailRequestBody) {
HttpHeaders httpHeaders = setHttpHeaders();
ResponseEntity<TransactionalEmailResponse> response = null;
try {
response = httpHandler.sendPost(endpoint, httpHeaders, emailRequestBody, TransactionalEmailResponse.class);
} catch (Exception e) {
logger.error("An error while sending POST request to email-service : " + JsonUtils.jsonize(emailRequestBody), e);
}
TransactionalEmailResponse body = response.getBody();
return body;
}
以下是相同的单元测试:
@InjectMocks
private APIKeyAuthHandler testAPIKeyAuthHandler;
@Mock
private HttpHandler httpHandler;
@Mock
private EmailServiceConfigProperties emailServiceConfigPropertiesMock;
@Test
public void testSendPostRequestForTransactionalEmail() {
TransactionalEmail transactionalEmail = getTransactionalEmailRequestBody(); //returns POJO
TransactionalEmailResponse transactionalEmailResponse = transcationEmailResponse();
Mockito.doReturn(setHttpHeaders()).when(testAPIKeyAuthHandler).setHttpHeaders(); returns POJO
String sendEmailEndpoint = "https://dev.com/api/v2/send/email";
Mockito.when(httpHandler.sendPost(sendEmailEndpoint, setHttpHeaders(), transactionalEmail, TransactionalEmailResponse.class))
.thenReturn(new ResponseEntity<>(transactionalEmailResponse, HttpStatus.OK));
assertNotNull(testAPIKeyAuthHandler.sendPostRequestForTransactionalEmail(sendEmailEndpoint, transactionalEmail));
}
下面是错误日志:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
at service.api.handlers.APIKeyAuthHandlerUnitTest.testSendPostRequestForTransactionalEmail(APIKeyAuthHandlerUnitTest.java:42)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
有人可以帮助我解决相同的问题吗? 谢谢
I'm trying to run unit test for below function
@Autowired
private EmailServiceConfigProperties emailServiceConfigProperties;
@Autowired
private HttpHandler httpHandler;
public TransactionalEmailResponse sendPostRequestForTransactionalEmail(String endpoint, TransactionalEmail emailRequestBody) {
HttpHeaders httpHeaders = setHttpHeaders();
ResponseEntity<TransactionalEmailResponse> response = null;
try {
response = httpHandler.sendPost(endpoint, httpHeaders, emailRequestBody, TransactionalEmailResponse.class);
} catch (Exception e) {
logger.error("An error while sending POST request to email-service : " + JsonUtils.jsonize(emailRequestBody), e);
}
TransactionalEmailResponse body = response.getBody();
return body;
}
Below is the unit test of the same:
@InjectMocks
private APIKeyAuthHandler testAPIKeyAuthHandler;
@Mock
private HttpHandler httpHandler;
@Mock
private EmailServiceConfigProperties emailServiceConfigPropertiesMock;
@Test
public void testSendPostRequestForTransactionalEmail() {
TransactionalEmail transactionalEmail = getTransactionalEmailRequestBody(); //returns POJO
TransactionalEmailResponse transactionalEmailResponse = transcationEmailResponse();
Mockito.doReturn(setHttpHeaders()).when(testAPIKeyAuthHandler).setHttpHeaders(); returns POJO
String sendEmailEndpoint = "https://dev.com/api/v2/send/email";
Mockito.when(httpHandler.sendPost(sendEmailEndpoint, setHttpHeaders(), transactionalEmail, TransactionalEmailResponse.class))
.thenReturn(new ResponseEntity<>(transactionalEmailResponse, HttpStatus.OK));
assertNotNull(testAPIKeyAuthHandler.sendPostRequestForTransactionalEmail(sendEmailEndpoint, transactionalEmail));
}
Below is the log of the error:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
at service.api.handlers.APIKeyAuthHandlerUnitTest.testSendPostRequestForTransactionalEmail(APIKeyAuthHandlerUnitTest.java:42)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131)
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
Can someone help me with how to fix the same?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里:
但是:
testAPIKeyAuthHandler
不是模拟,并且执行when()
规范的语法不会改变这一点。含义:@InjectMocks 注释不创建模拟。它创建一个对象,Mockito 将尝试用您的模拟对象填充!
但请注意,这里的真正答案是:这些事情很复杂。你无法通过反复试验来学习如何将mockito与spring这样的复杂框架结合使用。因此:你最好退后一步。选择一个关于如何使用mockito的好教程。阅读本文,并了解如何正确使用 Mockito。 然后拿起一个解释 Mockito 与 spring 结合的教程,阅读并理解它。然后然后将您所学到的知识应用到您的设置中。
Here:
But:
testAPIKeyAuthHandler
is not a mock, and the syntax for doing awhen()
specification will not change that.Meaning: the @InjectMocks annotation does not create a mock. It creates an object that Mockito will try to populate with your mock objects!
But note, the real answer here: these things are complicated. You can't learn how to use mockito in conjunction with a complex framework like spring by trial and error. Thus: you better step back. Pick up a good tutorial on how to use mockito. Read that, and understand how to use Mockito the proper way. Then pick up a tutorial that explains Mockito in conjunction with spring, read that, understand it. And then apply what you have learned to your setup.