如何解决如何避免使用 EasyMock 模拟接口默认方法?
我有一个具有默认方法的接口。这些指的是非默认方法,我只想模拟那些。
示例:
public interface InterfaceWithDefaultMethod {
public String getTestString();
public default String getCombinedString() {
return "Test" + getTestString();
}
}
我只想模拟getTestString()
EasyMock.mock(InterfaceWithDefaultMethod.class)
确实生成了一个模拟类,其中所有方法都被模拟,即 getCombinedString
没有被委托给 getTestString()
。
解决方法
这个问题可以通过创建接口的虚拟实现并部分模拟这个虚拟实现来解决。
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);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。