gpt4 book ai didi

java - Spring-Boot:Mockito Captor:NullPointerException 和 InvalidUseOfMatchersException

转载 作者:行者123 更新时间:2023-12-02 03:22:08 28 4
gpt4 key购买 nike

我试图熟悉 Mockito Captor,但我同时收到 NullPointerException 和 InvalidUseOfMatchersException。我有一种感觉,我做错了什么和/或错误地理解了捕获者技术。

这是我的测试类(class):

@Test
public void testFindByClientIdAndBatchDateBetween() {

DateTime todayDateTime = new DateTime().withTimeAtStartOfDay();

mockedTransactRepViewModel.capture().setClientId("123456");
mockedTransactRepViewModel.capture().setBatchDate(todayDateTime.toDate());
mockTransactRepViewRepository.save(mockedTransactRepViewModel.capture());

verify(mockTransactRepViewRepository).findByClientIdAndClDateBetween("123456", todayDateTime.toDate(),
todayDateTime.plusDays(1).toDate()).get(0);

String mockClientId = mockedTransactRepViewModel.getValue().getClientId();
assertThat(mockClientId.equals("123456"));

verify(mockTransactRepViewRepository, times(1)).save(mockedTransactRepViewModel.capture());
}

编辑:完整的堆栈跟踪:

java.lang.NullPointerException
at domain.TransactRepViewRepositoryTest.testFindByClientIdAndBatchDateBetween(TransactRepViewRepositoryTest.java:84)
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.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:16)
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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
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 com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)


org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced argument matcher detected here:

-> at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))

Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
Mocking methods declared on non-public parent classes is not supported.

最佳答案

捕获者的工作方式并不完全像你所拥有的那样。您应该仅在 verify 调用期间调用 capture 来代替参数,然后使用 getValue 检索捕获的一个或多个值>获取值

/* BAD */  mockedTransactRepViewModel.capture().setClientId("123456");

在没有捕获器的世界,如果您需要检查作为与模拟交互的一部分接收到的参数值,则需要使用 Mockito 匹配器(eq gt 例如),或者您需要编写 Hamcrest 匹配器来检查您收到的值类型。

/* in your system under test */
someSystem.setPowerLevel(9001);
someSystem.interact(new Widget(4));

/* in your test */
verify(mockSystem).setPowerLevel(gt(9000)); // greater than 9000
verify(mockSystem).interact(argThat(isAWidgetWithParameter(4)));

自然,这使得检查对象的多个属性或获取可在其他地方传递的实例的引用变得困难。这就是捕获器的用武之地,捕获器可以通过 ArgumentCaptor.forClass 创建,也可以通过 @Captor 注释创建。

/* in your system under test */
someSystem.interact(new Widget(4));

/* in your test */
verify(mockSystem).interact(widgetCaptor.capture());
Widget widgetInteractedWith = widgetCaptor.getValue();
assertEquals(4, widgetInteractedWith.getParameter());
doOtherAssertionsOn(widgetInteractedWith);

在幕后capture 方法实际上返回 null,因为您永远不会与该方法的返回值进行交互。与其他 Mockito 匹配器一样,该调用返回一个虚拟值,并且 Mockito 设置一些内部状态,以便它知道填充 Captor,而不是尝试对每个参数进行相等匹配。我在 how Mockito matchers work 上写了一个较长的答案,其中解释了有关此隐藏堆栈的更多信息。

关于java - Spring-Boot:Mockito Captor:NullPointerException 和 InvalidUseOfMatchersException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39469658/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com