I have an interface that has about 20 methods (huge).
I want to create a single instance for testing purposes, but I only need to override one method. What’s a good way to get an instance of this class with the overridden method without having to define the whole class with a tone of “//TODO: implement this” methods.
Mocking frameworks might work too, but I might prefer something that uses refection. I can create a mock object, but then can I override the method in the mock object?
example using EasyMock classextension framework
the class under test:
public boolean hasFiles() {
return dir();
}}
Test:
public class ATest {
public static void main(String[] args) throws Exception {
Class class1 = TestClass.class;
TestClass mock = createMock(class1, class1.getMethod("dir"));
expect(mock.dir()).andReturn(true).times(2);
expect(mock.dir()).andReturn(false).times(2);
replay(mock);
System.out.println("mock.dir()=" + mock.dir());
System.out.println("mock.hasFiles()=" + mock.hasFiles());
System.out.println("mock.dir()=" + mock.dir());
System.out.println("mock.hasFiles()=" + mock.hasFiles());
}
}
output: