小编des*_*est的帖子

使用 okhttp3 调用测试方法

我在服务类中有一个方法可以调用外部 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)

如果有助于测试,我愿意重构服务类。如有任何建议和推荐,我们将不胜感激。谢谢。

java unit-testing mocking okhttp spring-boot

4
推荐指数
1
解决办法
8374
查看次数

标签 统计

java ×1

mocking ×1

okhttp ×1

spring-boot ×1

unit-testing ×1