Java with mockito fails
I have following method in my service to mock AWS sqs
@Override
public Message recieveMessage(String queueUrl) {
Objects.requireNonNull(queueUrl);
ReceiveMessageRequest request = new ReceiveMessageRequest().withQueueUrl(queueUrl).withMaxNumberOfMessages(1);
ReceiveMessageResult receiveMessageResult = this.sqsClient.receiveMessage(request);
// As per the spec, we need to return only one message.
return receiveMessageResult.getMessages().get(0);
}
@Override
public int getMessageCount(String queueUrl) {
GetQueueAttributesResult queueAttributes = sqsClient.getQueueAttributes(queueUrl, Arrays.asList("ApproximateNumberOfMessages"));
return Integer.valueOf(queueAttributes.getAttributes().get("ApproximateNumberOfMessages"));
}
and following are my test cases for these methods using mockito are failing with NPE.
@Test
public void testRecieveMessage() {
Message message = new Message();
message.setBody("Message Body");
List<Message> messages = new ArrayList<>();
messages.add(message);
ReceiveMessageResult result = new ReceiveMessageResult();
result.setMessages(messages);
when(mock(ReceiveMessageResult.class).getMessages()).thenReturn(messages);
when(mock(List.class).get(0)).thenReturn(message);
when(this.amazonSQSClient.receiveMessage(mock(ReceiveMessageRequest.class))).thenReturn(result);
this.amazonQueueService.recieveMessage(anyString());
verify(this.amazonSQSClient, times(1)).receiveMessage(mock(ReceiveMessageRequest.class));
//Assert.assertNotNull(msg);
}
java.lang.NullPointerException at com.example.queue.service.impl.AmazonSQSService.recieveMessage(AmazonSQSService.java:46) at com.example.AmazonSQSServiceTest.testRecieveMessage(AmazonSQSServiceTest.java:77) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.ru nners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37) at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org .eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
@Test
public void testMessageCount() {
GetQueueAttributesResult result = new GetQueueAttributesResult();
result.addAttributesEntry("ApproximateNumberOfMessages", "10");
List<String> attrs = Arrays.asList("ApproximateNumberOfMessages");
when(this.amazonSQSClient.getQueueAttributes(anyString(), eq(attrs))).thenReturn(result);
this.amazonQueueService.getMessageCount(anyString());
}
org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 recorded: -> at com.example.AmazonSQSServiceTest.testMessageCount(AmazonSQSServiceTest.java:96)
This exception may occur if matchers are combined with raw values: //incorrect: someMethod(anyObject(), "raw String"); When using matchers, all arguments have to be provided by matchers. For example: //correct: someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
at com.example.queue.service.impl.AmazonSQSService.getMessageCount(AmazonSQSService.java:58)
at com.example.AmazonSQSServiceTest.testMessageCount(AmazonSQSServiceTest.java:96)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Am I missing something?
Here is what your code does:
ReceiveMessageRequest request = new ReceiveMessageRequest().withQueueUrl(queueUrl).withMaxNumberOfMessages(1);
ReceiveMessageResult receiveMessageResult = this.sqsClient.receiveMessage(request);
So it creates a new request, and passes ths new request to sqsClient.receiveMessage()
.
Here's how you test that:
when(this.amazonSQSClient.receiveMessage(mock(ReceiveMessageRequest.class))).thenReturn(result);
So your test tells th mock client to return result
when receiveMessage()
is called with a mock ReceiveMessageRequest.
So that can't work. The mock ReceiveMessageRequest is not equal to the new request used in the code. You need to do something like
when(this.amazonSQSClient.receiveMessage(any(ReceiveMessageRequest.class))).thenReturn(result);
So that, whetever the request passed to receiveMessage, the mock client returns the result.
Regarding your second question:
this.amazonQueueService.getMessageCount(anyString());
doesn't make sense. You need to call your method with a real, given string.
链接地址: http://www.djcxy.com/p/90372.html上一篇: 图结构junit测试NullPointerException
下一篇: Java与mockito失败