鉴于此目标代码:
...
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。
MockitoExample.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后,另外一条信息被曝光 - 如果Date是bindVariables
传递的唯一元素,则测试工作正常。也就是说,删除&#34; var1&#34;从目标和测试代码,然后测试在1.9.5和1.10.8下运行良好。此外,捕获者用于约会并不重要。如果参数属于另一种类型,例如Integer,则会出现同样的问题。