Reputation: 151
I have an interface having default methods. These refer to non-default methods and I want to mock only those.
Example:
public interface InterfaceWithDefaultMethod {
public String getTestString();
public default String getCombinedString() {
return "Test" + getTestString();
}
}
I only want to mock getTestString()
EasyMock.mock(InterfaceWithDefaultMethod.class)
does produce a mock class where all methods are mocked, i.e. getCombinedString
is not being delegated to getTestString()
.
Upvotes: 1
Views: 601
Reputation: 151
The problem can be solved by creating a dummy implementation of the interface and to partial-mock this dummy implementation.
private static class InterfaceMock implements InterfaceWithDefaultMethod {
@Override
public String getTestString() {
return null;
}
}
private InterfaceWithDefaultMethod testedInterface;
@Before
public void setup() {
testedInterface = createMockBuilder(InterfaceMock.class)
.addMockedMethod("getTestString")
.createMock();
resetToNice(testedInterface);
expect(testedInterface.getTestString()).andStubReturn("First");
replay(testedInterface);
}
Upvotes: 1