Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导:Mockito捕获器:NullPointerException和InvalidUseofMatcherException_Java_Unit Testing_Spring Boot_Mockito - Fatal编程技术网

Java Spring引导:Mockito捕获器:NullPointerException和InvalidUseofMatcherException

Java Spring引导:Mockito捕获器:NullPointerException和InvalidUseofMatcherException,java,unit-testing,spring-boot,mockito,Java,Unit Testing,Spring Boot,Mockito,我试图熟悉Mockito Captor,但我同时得到NullPointerException和InvalidUseofMatcherException。我有一种感觉,我做错了什么和/或不正确地理解了捕获者的技术 这是我的测试课: @Test public void testFindByClientIdAndBatchDateBetween() { DateTime todayDateTime = new DateTime().withTimeAtStartOfDay(); m

我试图熟悉Mockito Captor,但我同时得到NullPointerException和InvalidUseofMatcherException。我有一种感觉,我做错了什么和/或不正确地理解了捕获者的技术

这是我的测试课:

@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.

绑架者的工作方式和你的不一样。您应该只调用
capture
verify
调用期间代表参数,然后使用
getValue
getValues
检索捕获的值

/* 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,而不是尝试相等匹配每个参数。我在上写了一个较长的答案,这解释了有关这个隐藏堆栈的更多信息。

捕获者的工作方式与您的工作方式不同。您应该只调用
capture
verify
调用期间代表参数,然后使用
getValue
getValues
检索捕获的值

/* 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,而不是尝试相等匹配每个参数。我在上写了一个较长的答案,这解释了有关此隐藏堆栈的更多信息。

在上面添加完整的stacktrace或messagesEdited my post。在上面添加完整的stacktrace或messagesEdited my post。您好,谢谢您的回答,但我在理解它时有点困难。我这样解决了我的问题:
ArgumentCaptor Transact-RepViewModelArgumentCaptor=ArgumentCaptor.forClass(Transact-RepViewModel.class);验证(MockTransact-RepViewRepository,次(1)).save(Transact-RepViewModelArgumentCaptor.capture());assertEquals(“123456”,Transact-RepViewModelArgumentCaptor.getValue().getClientId())但是我不确定我对这个测试是否满意。我认为我必须转向集成测试,尽管我的db管理员反对它。@Deniss您的新测试看起来不错。重要的一点是,您只能从
验证
的方法调用中调用
capture
,然后使用
getValue
与捕获的值交互。至于您的测试范围,这取决于您和您的团队根据测试系统的总合同来决定。您好,谢谢您的回答,但我很难理解它。我这样解决了我的问题:
ArgumentCaptor Transact-RepViewModelArgumentCaptor=ArgumentCaptor.forClass(Transact-RepViewModel.class);验证(MockTransact-RepViewRepository,次(1)).save(Transact-RepViewModelArgumentCaptor.capture());assertEquals(“123456”,Transact-RepViewModelArgumentCaptor.getValue().getClientId())但是我不确定我对这个测试是否满意。我认为我必须转向集成测试,尽管我的db管理员反对它。@Deniss您的新测试看起来不错。重要的一点是,您只能从
验证
的方法调用中调用
capture
,然后使用
getValue
与捕获的值交互。至于您的测试范围,这取决于您和您的团队根据测试系统的总合同来决定。