为什么将 Mockito 从 1.9.5 升级到 1.10.8 会破坏这个俘虏



给定此目标代码:

...
sessionWrapper.execute(arenaCreateCql, arenaGuid, arenaName, displayName, authorName, createdOn);
...

和 Mockito 代码来验证该行:

...
@Captor
private ArgumentCaptor<Date> createdOnCaptor;
...
@Test
public void testThat_Execute_CreatesNewArena() throws Exception {
    ...
    inOrder.verify(mockSessionWrapper).execute(
        eq(arenaCreateCql), eq(testArenaGuid), eq(testArenaName), eq(testArenaDisplayName), eq(testAuthorName), createdOnCaptor.capture());
    ...
    assertNotNull(createdOnCaptor.getValue());
}

这使用 Mockito 1.9.5 工作。升级 1.10.8 时,验证通过,但 getValue() 失败并显示此错误:

org.mockito.exceptions.base.MockitoException: 
No argument value was captured!
You might have forgotten to use argument.capture() in verify()...
...or you used capture() in stubbing but stubbed method was not called.
Be aware that it is recommended to use capture() only with verify()

编辑以添加 MCVE。以下代码在 Mockito 1.9.5 中为绿色,在 Mockito 1.10.8 中为红色。

Mockito示例.java:

package org.makeyourcase.example;
import java.util.Date;
public class MockitoExample {
    private MockitoExampleExecutor executor;
    public void execute(){
        executor.execute("var1", new Date());
    }
}

MockitoExampleExecutor.java:

package org.makeyourcase.example;
public class MockitoExampleExecutor {
    public void execute(Object... bindVariables){
    }
}

MockitoExample_UT:

package org.makeyourcase.example;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
@RunWith(MockitoJUnitRunner.class)
public class MockitoExample_UT {
    @Mock
    private MockitoExampleExecutor mockitoExampleExecutor;
    @Captor
    private ArgumentCaptor<Date> dateCaptor;
    @InjectMocks
    private MockitoExample subject;
    @Test
    public void testThat_Execute_InvokesCalendar() throws Exception {
        subject.execute();
        verify(mockitoExampleExecutor).execute(eq("var1"), dateCaptor.capture());
        assertNotNull(dateCaptor.getValue());
    }
}
创建

MCVE 后,另一条信息浮出水面 - 如果日期是bindVariables传递的唯一元素,则测试工作正常。也就是说,从目标和测试代码中删除"var1",然后测试在 1.9.5 和 1.10.8 下运行良好。此外,捕获者是否用于约会并不重要。如果参数是另一种类型,如整数,则会出现相同的问题。

谢谢,这可能是一个错误,我已经创建了关于 GH-188 的报告。

不确定何时会修复。在 GH-211 中修复。

相关内容

  • 没有找到相关文章

最新更新