fmp*_*dmb 3 java unit-testing easymock
我正在尝试使用EasyMock和TestNG编写一些单元测试,并遇到了一个问题.鉴于以下内容:
void execute(Foo f) {
Bar b = new Bar()
b.setId(123);
f.setBar(b);
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试以下列方式测试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();
}
Run Code Online (Sandbox Code Playgroud)
在我的测试中,我不能只是调用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(), ???);
}
Run Code Online (Sandbox Code Playgroud)