如何防止调用测试的另一种方法一种方法?

问题描述 投票:0回答:1
public void makeLoginRequest(){
    view.log(sessionHandler.getEncodedCredentials());
    Call loginCall = apiService.getLoginInfo("application/json", "application/json"
            , "SPT", "Android", sessionHandler.getEncodedCredentials());

   loginCall.enqueue(new Callback<User>() {
        @Override
        public void onResponse(Call<User> call, Response<User> response) {
            handleLoginResponse(response);
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {
            handleLoginFailure(t);
        }
    });
}

我想,以测试这种方法使用JUnit和的Mockito。这种方法属于在演示者类。为了测试这个我跑presenter.makeLoginRequest();那么当onResponse叫我用verify(presenter).handleLoginResponse(response);永远也不会被调用。问题是,它会继续运行在handleLoginResponse(response);一切。我不想在此方法执行什么,而只需要验证此方法被调用。我怎么能够无视法的执行,或者说是测试的最佳方式?

java android junit mockito robolectric
1个回答
1
投票

有这样做的2种方式:

  1. 让您的演讲成为mock对象
presenter = mock<Presenter>()
  1. 添加到您的测试
doNothing().when(presenter).handleLoginResponse(any()); 
© www.soinside.com 2019 - 2024. All rights reserved.