我有一个类似
的代码//src
public class ClassToCapture {
private int x;
// setters, getters , constructors, etc ..
}
public class ClassToTest {
private Sender sender;
// setters, getters , constructors, etc ..
public functionToTest() {
ClassToCapture classToCapture = new ClassToCapture(0);
sender.send(classToCapture);
classToCapture.setX(1);
// some other operation
}
}
我想捕获由sender.send()发送的值,并检查它。所以我的测试用例看起来像这样。
//test
public class Test {
private Sender sender = mock(Sender.class);
private ClassToTest target = new ClassToTest(sender);
public void test() {
target.functionToTest();
ArgumentCaptor<ClassToCapture> argumentCaptor = ArgumentCaptor.forClass(ClassToCapture);
verify(sender).send(argumentCaptor.capture());
System.out.println(argumentCaptor.getValue().getX()); //printing 1, instead of 0, the "live-value" when the sender.send() was called.
}
}
似乎ArgumentCaptor
正在捕获要传递的对象的引用。有人知道还有其他方法吗?还是我做错了?