Eg let's say I have this class:
public class Foo Implements Fooable {
public void a() {
// does some stuff
bar = b();
// moar coadz
}
public Bar b() {
// blah
}
// ...
}
And I want to test Foo.a
. I want to mock Foo.b
, because I'm testing that method separately. What I'm imagining is something like this:
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);
}
}
I know I can write my own Foo
subclass to do this sort of thing for me. But I don't want to do that if I don't have to, because it's tedious ie should be automated.
In EasyMock 3.0+, you can create Partial mock using the mockbuilder
EasyMock.createMockBuilder(class).addMockedMethod("MethodName").createMock();
I guess you can do that using the EasyMock extensions library. You can find a simple example here in this Partial Mocking
The OP appears(?) to be suggesting that subclassing is somehow more difficult or tedious than partial mocking. I suggest it's worth rethinking that.
For example, in the test class:
Foo dummyFoo = new Foo() {
@Override public Bar b() { return new Bar(); }
};
does what the OP states, seems simpler, and less prone to other problems (forgetting to replay/verify/etc.), than using EasyMock.
I would find a way to upgrade to JUnit 4, and use classextensions. (Actually, I would use Mockito instead of EasyMock, but let's not rewrite your whole test suite.) If you can't, then you could always create your own spy thusly:
public class FooTest extends TestCase {
public static class FooSpy extends Foo {
private final Fooable mockFoo;
FooSpy(Fooable mockFoo) {
this.mockFoo = mockFoo;
}
public Bar b() {
return mockFoo.b();
}
}
public void testA() {
Fooable mockFoo = createMock(Foo.class);
Fooable fooSpy = new FooSpy(mockFoo);
// 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(mockFoo.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(mockFoo);
foo.a();
verify(foo);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.