如何为JUnit测试模拟OkHttp响应

huangapple go评论46阅读模式
英文:

how to mock okhttp response for JUnit test

问题

我正在通过okhttp向第三方API发出传出的HTTP请求:

public @Nullable result Call3rdParty {
    OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
        .readTimeout(RW_TIMEOUT, TimeUnit.MILLISECONDS)
        .retryOnConnectionFailure(true)
        .build();

    Request request = new Request.Builder()
       .url(url)
       .build();
    Response response = client.newCall(request).execute();

    //反序列化和进行轻微数据操作...
}

我想创建一个单元测试并模拟响应。

private MockWebServer server;

@Before
public void setUp() throws IOException {
  this.server = new MockWebServer();
  this.server.start();
}

@After
public void tearDown() throws IOException {
  this.server.shutdown();
}

@Test
public void Test_SUCCESS() throws Exception {
  String json = readFileAsString(file);
  this.server.enqueue(new MockResponse().setResponseCode(200).setBody(json));
  //TODO: 在这里做什么??
}

在模拟响应被排队后,我需要做什么来返回一个模拟响应并在我正在测试的方法的其余部分中使用它?

英文:

I'm making an outbound HTTP request to a 3rd party API via okhttp:

public @Nullable result Call3rdParty {
    OkHttpClient client = new OkHttpClient.Builder()
        .connectTimeout(CONNECTION_TIMEOUT, TimeUnit.MILLISECONDS)
        .readTimeout(RW_TIMEOUT, TimeUnit.MILLISECONDS)
        .retryOnConnectionFailure(true)
        .build();
    

    Request request = new Request.Builder()
       .url(url)
       .build();
    Response response = client.newCall(request).execute();

    //Deserialize and do minor data manipulation...
}

I want to create a unit test and mock the responses.

  private MockWebServer server;

  @Before
  public void setUp() throws IOException {
    this.server = new MockWebServer();
    this.server.start();
  }

  @After
  public void tearDown() throws IOException {
    this.server.shutdown();
  }

  @Test
  public void Test_SUCCESS() throws Exception {
    String json = readFileAsString(file);
    this.server.enqueue(new MockResponse().setResponseCode(200).setBody(json));
    //TODO: What to do here??
   }

After a mock response has been enqueued, what do I need to do return a mock response and use it in the remaining part of the method I'm testing?

答案1

得分: 1

这个项目的文档涵盖了这部分内容:

https://github.com/square/okhttp/tree/master/mockwebserver

  // 询问服务器其URL。您需要这个来进行HTTP请求。
  HttpUrl url = server.url("/myendpoint");

  // 在这里调用您的客户端代码,将服务器位置传递给它
  response = Call3rdParty(url)
英文:

The project documentation covers this

https://github.com/square/okhttp/tree/master/mockwebserver

  // Ask the server for its URL. You'll need this to make HTTP requests.
  HttpUrl url = server.url("/myendpoint");

  // Call your client code here, passing the server location to it
  response = Call3rdParty(url)

huangapple
  • 本文由 发表于 2020年10月1日 00:55:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64142296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定