我正在尝试使用mockito进行单元测试,但是测试失败我得到了这个:
java.lang.ClassCastException:java.lang.Boolean无法强制转换为 OnDataListListener
此行:mPresenter.getUsers(isLoad, forceUpdate);
测试:
doAnswer(answerVoid(
(OnDataListListener<List<User>> myCallback) -> myCallback.onSuccess(mList))).when(
dataInteractor).getUsers(anyBoolean(), any(DataListener.class));
mPresenter.getUsers(isLoad, forceUpdate);
演示
@Override public void getUsers(boolean isLoad, boolean update) {
this.isLoad = isLoad;
this.forceUpdate = update;
dataInteractor.performgetUsers(update, this);//this refer to the listener
}
DataInteractor:
public void performGetGetUsers(boolean update, OnDataListListener callback) {
mDataHandler.getUsers(update, new Observer<List<User>>() {
....
侦听器界面:
public interface DataListener<T> {
void onSuccess(T data);
void onFailure(Throwable e);
}
答案 0 :(得分:0)
这太奇怪了,但转换参数解决了这个问题!
dataInteractor.performgetUsers(update, this);//this refer to the listener
到
dataInteractor.performgetUsers(this, update);//this refer to the listener