- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图熟悉 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/
我正在处理一些不能很好地处理异常的旧代码。我正在编写的一个测试对引发异常的方法进行 stub ,我需要确认该异常是正确的。我可以看到测试此异常的唯一方法是在记录参数时捕获参数并比较字符串。 当我运行这
我需要在 Mockito 中验证一个具有多个参数的方法,但只需要捕获一个参数,其他我只需要一个简单的匹配器。这可能吗? 例如,如果我有: @Mock private Map mockedMap; ..
我正在尝试使用 @Mock 编写测试和 @Captor注释。但是,对象没有被创建,所以我得到 NullPointerExceptions . 考试: @RunWith(MockitoJUnitRunn
我正在使用 ArgumentCaptor与 @Captor像这样在 Kotlin 中注释 @Captor private lateinit var captor: ArgumentCaptor @Mo
我们使用 gopkg.in/mgo.v2/bson 与 mongo 对话,它的 API 填充传递的结构而不是返回结果,例如: func (p *Pipe) One(result interface{}
我试图熟悉 Mockito Captor,但我同时收到 NullPointerException 和 InvalidUseOfMatchersException。我有一种感觉,我做错了什么和/或错误地
我正在使用 Spock 和 Mockito 框架编写单元测试,并且偶然发现了 Mockito 中的一个我无法优雅解决的限制。 以下代码解析 .csv 文件并返回 TradedInstrument 对象
给定这个目标代码: ... sessionWrapper.execute(arenaCreateCql, arenaGuid, arenaName, displayName, authorName,
我是一名优秀的程序员,十分优秀!