我在服务类中有一个方法可以调用外部 api。我将如何模拟这个 okHttpClient 调用?我尝试用mockito 这样做,但没有成功。
//this is the format of the method that i want to test
public string sendMess(EventObj event) {
OkHttpClient client = new OkHttpClient();
//build payload using the information stored in the payload object
ResponseBody body =
RequestBody.create(MediaType.parse("application/json"), payload);
Request request = //built using the Requestbody
//trying to mock a response from execute
Response response = client.newCall(request).execute();
//other logic
}
Run Code Online (Sandbox Code Playgroud)
如果有助于测试,我愿意重构服务类。如有任何建议和推荐,我们将不胜感激。谢谢。