如何使用相同的参数验证对同一模拟方法的调用,该参数在模拟中的调用之间改变状态?
我有以下代码要进行单元测试:
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.
相关文章