问题描述
我有以下代码要进行单元测试:
I have the following code to be unit tested:
public void foo() {
Entity entity = //...
persistence.save(entity);
entity.setDate(new Date());
persistence.save(entity);
}
我想验证在第一次调用 persistence.save entity.getDate() 时返回 null.
I would like to verify that on the first invocation of persistence.save entity.getDate() returns null.
因此我无法使用 Mockito.verify(/*...*/) 因为当时方法 foo 已经完成并且 entity.setDate(Date) 被调用.
Therefore I'm unable to use Mockito.verify(/*...*/) because at that time the method foo already completed and entity.setDate(Date) was called.
所以我认为我需要在调用发生时对调用进行验证.如何使用 Mockito 做到这一点?
So I think I need to do verifications of invocations already at the time the invocations happen. How do I do this using Mockito?
推荐答案
我创建了以下 Answer 实现:
I created the following Answer implementation:
public class CapturingAnswer<T, R> implements Answer<T> {
private final Function<InvocationOnMock, R> capturingFunction;
private final List<R> capturedValues = new ArrayList<R>();
public CapturingAnswer(final Function<InvocationOnMock, R> capturingFunction) {
super();
this.capturingFunction = capturingFunction;
}
@Override
public T answer(final InvocationOnMock invocation) throws Throwable {
capturedValues.add(capturingFunction.apply(invocation));
return null;
}
public List<R> getCapturedValues() {
return Collections.unmodifiableList(capturedValues);
}
}
此答案捕获正在执行的调用的属性.capturedValues 然后可以用于简单的断言.该实现使用 Java 8 API.如果这不可用,则需要使用能够将 InvocationOnMock 转换为捕获值的接口.测试用例中的用法是这样的:
This answer captures properties of the invocations being made. The capturedValues can then be used for simple assertions. The implementation uses Java 8 API. If that is not available one would need to use an interface that is able to convert the InvocationOnMock to the captured value. The usage in the testcase is like this:
@Test
public void testSomething() {
CapturingAnswer<Void,Date> captureDates = new CapturingAnswer<>(this::getEntityDate)
Mockito.doAnswer(captureDates).when(persistence).save(Mockito.any(Entity.class));
service.foo();
Assert.assertNull(captureDates.getCapturedValues().get(0));
}
private Date getEntityDate(InvocationOnMock invocation) {
Entity entity = (Entity)invocation.getArguments()[0];
return entity.getDate();
}
使用 Mockitos ArgumentCaptor 无法实现由呈现的 Answer 实现完成的捕获,因为这仅在调用被测方法后使用.
The capturing that is done by the presented Answer implementation can't be achieved with Mockitos ArgumentCaptor because this is only used after the invocation of the method under test.
这篇关于如何使用相同的参数验证对同一模拟方法的调用,该参数在模拟中的调用之间改变状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)