我有一个实现回调的接口:-
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("测试",MainClass $ 1 @7c03092a):OtherClass。gettotherdata ("test", EasyMock for interface Callback):期望:1,实际:0
我正在使用EasyMock和PowerMock的组合。任何帮助或指点都将大有帮助。提前感谢
在测试用例中按下面一行操作:-
otherClass.getOtherData(EasyMock.isA(String.class), EasyMock.isA(Callback.class));