java 在Mockito第一次呼叫后身体React为空

kmynzznz  于 2023-05-05  发布在  Java
关注(0)|答案(1)|浏览(117)

我在应用程序中使用Mockito模拟OkHttp类。第一次mock运行良好,我在body中得到"foo"作为响应,但第二次是空的。我试着调试它,看起来模拟是工作,我不知道为什么它不为第二次调用。

我的测试:

@Test
void get500() throws IOException {
    String responseMessage = "foo";
    int responseCode = 500;

    final OkHttpClient okHttpClient = mock(OkHttpClient.class);
    final Call remoteCall = mock(Call.class);
    final Response responseBuilder = new Response.Builder()
            .request(new Request.Builder().url(url).build())
            .protocol(Protocol.HTTP_1_1)
            .code(responseCode).message("").body(
                    ResponseBody.create(
                            responseMessage,
                            MediaType.parse("application/json")
                    ))
            .build();

    when(remoteCall.execute()).thenReturn(responseBuilder);
    when(okHttpClient.newCall(any())).thenReturn(remoteCall);
    
    MyHttp myHttp = new MyHttp(okHttpClient);
    ResponseServer response = myHttp.get("http://url.com");
    Assertions.assertEquals(response.getBody(), responseMessage);
}

下面是MyHttp类:

public class MyHttp {
    protected final OkHttpClient client;
    
    public MyHttp(OkHttpClient client) {
        this.client = client;
    }
    
    public ResponseServer get(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();
        
        Response response = client.newCall(request).execute();
        String responseBody = Objects.requireNonNull(response.body()).string();
        Assertions.assertEquals(response.getBody(), "foo");

        Response response2 = client.newCall(request).execute();
        String responseBody2 = Objects.requireNonNull(response2.body()).string();
        Assertions.assertEquals(response2.getBody(), "foo");  // response is empty
    }
}

编辑:

模仿回应如下,但不起作用:

Response mockResponse = mock(Response.class);
when(mockResponse.request()).thenReturn(new Request.Builder().url(url).build());
when(mockResponse.protocol()).thenReturn(Protocol.HTTP_1_1);
when(mockResponse.code()).thenReturn(responseCode);
when(mockResponse.message()).thenReturn("");
when(mockResponse.body()).thenReturn(
        ResponseBody.create(
                responseMessage,
                MediaType.parse("application/json")
        ));
gmxoilav

gmxoilav1#

我复制了您的示例,但做了一些更正(您的代码段有编译错误)
我的部分mavenpom.xml:

<dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>4.11.0</version>
    </dependency>
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-inline</artifactId>
      <version>4.5.1</version>
      <scope>test</scope>
    </dependency>

MyHttp类:

public class MyHttp {
  protected final OkHttpClient client;

  public MyHttp(OkHttpClient client) {
    this.client = client;
  }

  public Response get(String url) throws IOException {
    Request request = new Request.Builder()
        .url(url)
        .get()
        .build();

    Response response = client.newCall(request).execute();
    String responseBody = Objects.requireNonNull(response.body()).string();
    Assert.isTrue("foo".equals(responseBody));
    //Assertions.assertEquals(response.getBody(), "foo");

    Response response2 = client.newCall(request).execute();
    String responseBody2 = Objects.requireNonNull(response2.body()).string();
    //Assertions.assertEquals(response2.getBody(), "foo");  // response is empty
    Assert.isTrue("foo".equals(responseBody2));
    return response2;
  }
}

测试类

@RunWith(MockitoJUnitRunner.class)
class MyHttpTest {

  @Test
  void get500() throws IOException {
    String responseMessage = "foo";
    int responseCode = 500;

    final OkHttpClient okHttpClient = mock(OkHttpClient.class);
    final Call remoteCall = mock(Call.class);
/*    final Response response = new Response.Builder()
        .request(new Request.Builder().url("http://url.com").build())
        .protocol(Protocol.HTTP_1_1)
        .code(responseCode).message("").body(
            ResponseBody.create(
                responseMessage,
                MediaType.parse("application/json")
            ))
        .build();*/

    Response mockResponse = mock(Response.class);
    ResponseBody mockResponseBody = mock(ResponseBody.class);

    when(mockResponseBody.string()).thenReturn("foo");
    when(mockResponse.body()).thenReturn(mockResponseBody);

    when(remoteCall.execute()).thenReturn(mockResponse);
    when(okHttpClient.newCall(any())).thenReturn(remoteCall);

    MyHttp myHttp = new MyHttp(okHttpClient);
    Response response = myHttp.get("http://url.com");
    Assertions.assertEquals(response.body().string(), responseMessage);
  }
}

相关问题