我有一个实现回调的界面: -
public interface Callback {
void printData(String data);
}
我想要编写单元测试用例的课程
public class MainClass{
public void getData(String data)
{
// here I am calling another class which accepts that interface as param and implementing it anonymously
otherClass.getOtherData(data, new new Callback() {
void printData(String data)
{
// do some logic with data
}
});
}
}
当我使用EasyMock编写MainClass的测试用例时,它有以下代码段
MainClass mainclass = new MainClass();
OtherClass otherClass = EasyMock.createMock(OtherClass.class);
Callback callback = EasyMock.createMock(Callback.class);
otherClass.getOtherData("test", callback);
EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
@Override
public Object answer() throws Throwable {
callback.printData(message);
return null;
}
}).once();
发出以下错误: -
意外的方法调用OtherClass.getOtherData(&#34; test&#34;,MainClass $ 1 @ 7c03092a): OtherClass.getOtherData(&#34; test&#34;,EasyMock for interface Callback):expected:1,actual:0
我正在使用EasyMock和PowerMock的组合。任何帮助或指示都会有很大帮助。提前致谢
答案 0 :(得分:0)
通过在测试用例中执行以下操作来实现它: -
otherClass.getOtherData(EasyMock.isA(String.class), EasyMock.isA(Callback.class));