我試圖測試我的網絡模塊。當我在模擬器或設備上運行它時,處理程序是可以的,但是當我試圖從測試中執行時,handler = null和callback不會被調用。我怎麼解決這個問題?在使用junit測試期間Android Handler爲null
public void performCall(Call callToPerform){
callToPerform.call.enqueue(new okhttp3.Callback() {
Handler handler = new Handler();
@Override
public void onFailure(okhttp3.Call call, IOException e) {
handler.post(() -> {
for (Callback callback : callToPerform.callbacks) {
callback.onFailure(callToPerform, e);
}
});
}
@Override
public void onResponse(okhttp3.Call call, final okhttp3.Response response){
handler.post(() -> {
for (Callback callback : callToPerform.callbacks) {
try {
callback.onResponse(callToPerform, new Response(response.body().bytes(), response.headers().toMultimap()));
} catch (IOException e) {
callback.onFailure(call, e);
}
}
});
}
});
}
我的分級應用程序文件包含此參數。
testOptions {
unitTests.returnDefaultValues = true
}
好文章,謝謝! – quaddef