【发布时间】:2020-02-26 03:47:43
【问题描述】:
我有如下 JUnit 测试:
@Autowired
MyService myservice;
@Before
public void init() {
myservice.doStuff(new MyObj());
}
@Test
public void test() {
ArgumentCaptor<MyObj> captor = ArgumentCaptor.forClass(MyObj.class);
myservice.doStuff(new MyObj());
verify(myservice, atLeastOnce()).doStuff(captor.capture());
captor.getAllValues(); //this returns 2 - one for the @Before and one for right above
}
正如注释代码中所提到的,即使捕获器是在 @Before 之后创建的,捕获器也会捕获这两个调用。为什么会这样?如何仅捕获测试中的调用?
【问题讨论】:
标签: spring junit mocking mockito