Java与mockito失败

我在服务中使用以下方法来模拟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"));
}

以下是我使用mockito的这些方法的测试用例与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在com.example.queue.service.impl.AmazonSQSService.recieveMessage(AmazonSQSService.java:46)在com.example.AmazonSQSServiceTest.testRecieveMessage(AmazonSQSServiceTest.java:77)在sun.reflect.NativeMethodAccessorImpl.invoke0(本地方法)在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)在java.lang.reflect.Method.invoke(Method.java:498) org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)的org.junit.runners.model。 FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)位于org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java: 26)org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)at org.junit.ru nners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)的组织。 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)在在org.junit.runners.ParentRunner.run(ParentRunner.java:363)在org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)。在org.eclipse.jdt的org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)上的org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62) 。org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)at org.internal.junit.runner.TestExecution.run(TestExecution.java:38) .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:使用参数匹配器无效! 预期2个匹配器,1个记录: - > at com.example.AmazonSQSServiceTest.testMessageCount(AmazonSQSServiceTest.java:96)

如果匹配器与原始值结合,则可能会发生此异常://不正确:someMethod(anyObject(),“raw String”); 在使用匹配器时,所有参数必须由匹配器提供。 例如:// correct:someMethod(anyObject(),eq(“String by matcher”));

欲了解更多信息,请参阅Matchers类的javadoc。

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)

我错过了什么吗?


以下是你的代码的作用:

ReceiveMessageRequest request = new ReceiveMessageRequest().withQueueUrl(queueUrl).withMaxNumberOfMessages(1);
ReceiveMessageResult receiveMessageResult = this.sqsClient.receiveMessage(request);

所以它创建一个新的请求,并将新的请求传递给sqsClient.receiveMessage()

以下是你如何测试:

when(this.amazonSQSClient.receiveMessage(mock(ReceiveMessageRequest.class))).thenReturn(result);

所以你的测试告诉模拟客户端在receiveMessage()被模拟ReceiveMessageRequest调用时返回result

所以这是行不通的。 模拟ReceiveMessageRequest不等于代码中使用的新请求。 你需要做类似的事情

when(this.amazonSQSClient.receiveMessage(any(ReceiveMessageRequest.class))).thenReturn(result);

因此,无论请求传递给receiveMessage,模拟客户端都会返回结果。

关于你的第二个问题:

this.amazonQueueService.getMessageCount(anyString());

没有意义。 你需要用一个真实的,给定的字符串来调用你的方法。

链接地址: http://www.djcxy.com/p/90371.html

上一篇: Java with mockito fails

下一篇: Logger with mockito in java