例如,假设我有这个类:
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
子类来完成这种操作。但如果不必要,我不想这样做,因为这很乏味, 应该是可以自动化的。
Bar
对象。 - raddykrish