Issue
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
Solution
Here:
when(testAPIKeyAuthHandler)
But:
@InjectMocks
private APIKeyAuthHandler testAPIKeyAuthHandler;
testAPIKeyAuthHandler
is not a mock, and the syntax for doing a when()
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.
Answered By - GhostCat
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)