我正在尝试使用 EasyMock 和 TestNG 编写一些单元测试,但遇到了一个问题。鉴于以下情况:
void execute(Foo f) {
Bar b = new Bar()
b.setId(123);
f.setBar(b);
}
我正在尝试测试 Bar 的 Id 是否按照以下方式相应设置:
@Test
void test_execute() {
Foo f = EasyMock.createMock(Foo.class);
execute(f);
Bar b = ?; // not sure what to do here
f.setBar(b);
f.expectLastCall();
}
在我的测试中,我不能只调用 f.getBar()
并检查它的 Id,因为 f
是一个模拟对象。有什么想法吗?这是我想查看 EasyMock v2.5 添加的 andDelegateTo()
和 andStubDelegateTo()
的地方吗?
哦,只是为了记录……EasyMock 的文档很糟糕。
最佳答案
啊哈!捕获是关键。
@Test
void test_execute() {
Foo f = EasyMock.createMock(Foo.class);
Capture<Bar> capture = new Capture<Bar>();
f.setBar(EasyMock.and(EasyMock.isA(Bar.class), EasyMock.capture(capture)));
execute(f);
Bar b = capture.getValue(); // same instance as that set inside execute()
Assert.assertEquals(b.getId(), ???);
}
关于java - 使用 EasyMock 测试参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2782937/