本文介绍了如何使用在mockito中调用之间更改状态的相同参数来验证相同模拟方法的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码进行单元测试:

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?

推荐答案

我创建了以下答案实施:

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 无法实现,因为这仅在调用被测方法后使用。

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.

这篇关于如何使用在mockito中调用之间更改状态的相同参数来验证相同模拟方法的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 05:26