Mockito:java.lang.ClassCastException:java.lang.Boolean不能强制转换为DataListListener

时间:2017-06-12 12:59:28

标签: java android mockito

我正在尝试使用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);
}

1 个答案:

答案 0 :(得分:0)

这太奇怪了,但转换参数解决了这个问题!

dataInteractor.performgetUsers(update, this);//this refer to the listener

dataInteractor.performgetUsers(this, update);//this refer to the listener