我正在尝试使用 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
}
数据交互器 :
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);
}
这太奇怪了,但是切换参数解决了问题!
dataInteractor.performgetUsers(update, this);//this refer to the listener
自
dataInteractor.performgetUsers(this, update);//this refer to the listener