Android 在方法单元测试中调用方法
我有一个演讲者要测试。Presenter包含一个方法,该方法在某个成功返回时调用另一个方法。现在我如何检查成功返回是否调用了该方法 下面是示例代码,它只是验证非mock对象的方法Android 在方法单元测试中调用方法,android,unit-testing,junit,mockito,android-testing,Android,Unit Testing,Junit,Mockito,Android Testing,我有一个演讲者要测试。Presenter包含一个方法,该方法在某个成功返回时调用另一个方法。现在我如何检查成功返回是否调用了该方法 下面是示例代码,它只是验证非mock对象的方法 tutorProfileDataReceived(tutorProfiledata)是我需要确保在响应时被调用的方法 实际上,这个模拟没有任何交互 Call call = apiInterface.getTutorProfile(tutorProfile); call.enqueue(new Callb
tutorProfileDataReceived(tutorProfiledata)
是我需要确保在响应时被调用的方法
实际上,这个模拟没有任何交互
Call call = apiInterface.getTutorProfile(tutorProfile);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
TutorProfile tutorProfiledata = (TutorProfile) response.body();
tutorProfileDataReceived(tutorProfiledata);
}
@Override
public void onFailure(Call call, Throwable t) {
Log.d(TAG,"getting suggestions call cancel");
call.cancel();
}
});
测试工作
@Before
public void setUp(){
MockitoAnnotations.initMocks(this);
apiInterface = NetworkContext.getInstance().getService(APIInterface.class);
presenter = Mockito.spy(new TutorProfilePresenter(viewMock , contextMock));
}
@Test
public void getTutorDataTest(){
int searchId = 2;
TutorProfile profileSender = new TutorProfile(searchId);
Call<TutorProfile> call = apiInterface.getTutorProfile(profileSender);
try {
Response<TutorProfile> response = call.execute();
TutorProfile profileReceiver = response.body();
Mockito.verify(presenter).tutorProfileDataReceived(profileReceiver);
} catch (IOException e) {
e.printStackTrace();
}
}
@之前
公共作废设置(){
initMocks(this);
apiInterface=NetworkContext.getInstance().getService(apiInterface.class);
presenter=Mockito.spy(新的TutorProfilePresenter(viewMock,contextMock));
}
@试验
public void getutordatatest(){
int searchId=2;
TutorProfile profileSender=新的TutorProfile(搜索ID);
Call Call=apinterface.gettuorprofile(profileSender);
试一试{
Response=call.execute();
TutorProfile profileReceiver=response.body();
Mockito.verify(演示者)、tutorProfileDataReceived(profileReceiver);
}捕获(IOE异常){
e、 printStackTrace();
}
}