例如假設我有這個類:有沒有辦法使用EasyMock部分模擬對象?
public class Foo Implements Fooable {
public void a() {
// does some stuff
bar = b();
// moar coadz
}
public Bar b() {
// blah
}
// ...
}
而且我想測試Foo.a
。我想模擬Foo.b
,因爲我正在單獨測試該方法。什麼我想象的是這樣的:
public class FooTest extends TestCase {
public void testA() {
Fooable foo = createPartialMock(
Fooable.class, // like with createMock
Foo // class where non-mocked method implementations live
);
// Foo's implementation of b is not used.
// Rather, it is replaced with a dummy implementation
// that records calls that are supposed to be made;
// and returns a hard coded value (i.e. new Bar()).
expect(foo.b()).andReturn(new Bar());
// The rest is the same as with createMock:
// 1. Stop recording expected calls.
// 2. Run code under test.
// 3. Verify that recorded calls were made.
replay(foo);
foo.a();
verify(foo);
}
}
我知道我可以寫我自己Foo
子做這樣的事情對我來說。但是如果我不必這樣做,我不想這樣做,因爲它很乏味,即應該是自動化的。
謝謝您創建部分模擬!不幸的是,你似乎需要Junit 4才能使用classextensions :(:http://easymock.org/EasyMock2_2_ClassExtension_Documentation.html我想這意味着Junit 3用戶運氣不好。 – allyourcode
Ohh ...這是一個很好的信息我也是......我對你的案例有一個想法...但它並沒有使用簡單的模擬,而是通過覆蓋這些方法來創建嘲笑,並從擴展的Mock類返回模擬'Bar'對象。 – raddykrish
從EasyMock 3.1開始,ClassExtensions庫已被棄用,部分模擬已被移植到EasyMock本身。這說明它可以與JUnit 3一起使用,所以你可能會很幸運:http://easymock.org/EasyMock3_1_Documentation.html – DoctorRuss