- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想测试以下代码:
pojo.setStatus(Status.INITIAL);
pojo.setExecutionCounter(0);
//eventRepository is a mock
scheduleEvent(this.eventRepository.save(pojo).get());
在单元测试中,我想验证这一点
INITIAL
,计数器为 0
不幸的是,我不知道如何做到这一点,因为我必须返回我必须捕获
的参数。当我展示单元测试的示例时,也许它会变得更加清晰:
succeededEvent.setEventStatus(INITIAL);
succeededEvent.setExecutionCounter(0);
//Actually we want here to return the argument
//which is captured by eventArgumentCaptor??
when(this.eventRepository.save(this.eventArgumentCaptor.capture()))
.thenReturn(succededEvent);
this.processor.processEvent(initialEvent);
Mockito.verify(this.eventRepository,
Mockito.times(1)).save(eventCaptureExecuteCaptor.capture());
final Event capturedEvent = eventCaptureExecuteCaptor.getValue();
//Counter and status should be reset
assertEquals(new Integer(0), capturedEvent.getExecutionCounter());
assertEquals(INITIAL, capturedEvent.getEventStatus());
verify(this.eventRepository,
times(1)).save(eq(eventCaptureExecuteCaptor.getValue()));
最佳答案
不知道为什么在 stub 时需要使用 captor。您可以在调用 SUT 之后按照设计使用它:
this.processor.processEvent(initialEvent);
Mockito.verify(this.eventRepository,
Mockito.times(1)).save(eventCaptureExecuteCaptor.capture());
stub 时,您可以直接获取所需的具体对象:
when(this.eventRepository.save(succededEvent)
.thenReturn(succededEvent);
或者如果您在设置时手头没有该对象,则使用通用输入:
when(this.eventRepository.save(anyClass(EventPojo.class))
.thenReturn(succededEvent);
编辑:
您还可以使用 thenAnswer
以及接受 Pojo 类型的任何输入类:
when(this.eventRepository.save(Mockito.any(EventPojo.class))
.thenAnswer(new Answer<EventPojo>(){
EventPojo pojo = invocation.getArgument(0);
return pojo;
}
);
由于这是一个匿名实现,如果需要,您可以捕获传递的 pojo 的状态。
关于java - Mockito 在 "recording"时间捕获参数并在稍后执行时使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48109107/
我正在编写一个插件,有一个ajax调用向用户显示数据。 如果用户想在ajax成功时添加一些js?他可以从他的 js 脚本中做到这一点吗,比如定位这个 ajax 成功事件。 例如: $(documen
我有 html 代码,例如 - x 最初插入 div 'insert_calendar_eda_form'。 Javascript代码 calendar_eda_add
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我已经使用命令 sudo start myservice 启动了一个 upstart 服务。我想要一种方法,以便稍后我(或任何人)可以检查该服务是否已启动并正在运行。检查它的命令是什么? 最佳答案 找
我是一名优秀的程序员,十分优秀!