0
在這裏我得到了演示者代碼示例。如何使在改造調用寫的onSuccess和onFailure處測試單元測試在回調改造
public void getNotifications(final List<HashMap<String,Object>> notifications){
if (!"".equalsIgnoreCase(userDB.getValueFromSqlite("email",1))) {
UserNotifications userNotifications =
new UserNotifications(userDB.getValueFromSqlite("email",1),Integer.parseInt(userDB.getValueFromSqlite("userId",1).trim()));
Call call = apiInterface.getNotifications(userNotifications);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
UserNotifications userNotifications1 = (UserNotifications) response.body();
if(userNotifications1.getNotifications().isEmpty()){
view.setListToAdapter(notifications);
onFailure(call,new Throwable());
}
else {
for (UserNotifications.Datum datum:userNotifications1.getNotifications()) {
HashMap<String,Object> singleNotification= new HashMap<>();
singleNotification.put("notification",datum.getNotification());
singleNotification.put("date",datum.getDate());
notifications.add(singleNotification);
}
view.setListToAdapter(notifications);
}
}
@Override
public void onFailure(Call call, Throwable t) {
call.cancel();
}
});
}
}
}
我怎樣寫單元測試涵蓋所有情況下,這段代碼。
感謝
三江源洙多豬頭...它真的救了我的 – Jay
天你能解釋一下我關於Mockito.doAnswer(東西..) – Jay
您使用符號指定仿製品的行爲。它類似於Mockito.when(mockObject).someMethod(any(Parameter.class))。thenReturn(returnValue);但是這個必須用於沒有(void)返回類型的函數。請參閱https://testing.googleblog.com/2014/03/whenhow-to-use-mockito-answer.html。 –