简体   繁体   中英

Mockito : java.lang.ClassCastException: java.lang.Boolean cannot be cast to DataListListener

I'm trying to perform an unit test using mockito, but the test failed I 'am getting this :

java.lang.ClassCastException: java.lang.Boolean cannot be cast to OnDataListListener

here in this line : mPresenter.getUsers(isLoad, forceUpdate);

The test :

 doAnswer(answerVoid(
        (OnDataListListener<List<User>> myCallback) -> myCallback.onSuccess(mList))).when(
        dataInteractor).getUsers(anyBoolean(), any(DataListener.class));

    mPresenter.getUsers(isLoad, forceUpdate);

Presenter

  @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>>() {
....

The listener interface:

public interface DataListener<T> {
  void onSuccess(T data);

  void onFailure(Throwable e);
}

This is so weird, but switching the params solved the issue !

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

to

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM