2

我已经设置了一个简单的测试控制器:

@Controller("/test")
public class SampleController {
  @Get(value = "1", produces = MediaType.TEXT_PLAIN)
  public String helloWorld1() {
    return "Hello, World!";
  }

  @Get(value = "2", produces = MediaType.TEXT_PLAIN)
  public HttpResponse<String> helloWorld2() {
    return HttpResponse.ok("Hello, World!");
  }
}

我在我的单元测试中使用了低级 HTTPClient,它看起来像这样:

@MicronautTest
public class SampleControllerTest {

  @Inject
  EmbeddedServer server;

  @Inject
  @Client("/test")
  HttpClient client;

  @Test
  void shouldReturnHelloWorld1_1() {
    HttpResponse<String> response = client.toBlocking().exchange(HttpRequest.GET("/1").accept(
        MediaType.TEXT_PLAIN));

    assertEquals(200, response.code());
    assertEquals("Hello, World!", response.body());
  }

  @Test
  void shouldReturnHelloWorld1_2() {
    String response = client.toBlocking().retrieve(HttpRequest.GET("/1").accept(MediaType.TEXT_PLAIN));

    assertEquals("Hello, World!", response);
  }

  @Test
  void shouldReturnHelloWorld2() {
    HttpResponse<String> response = client.toBlocking().exchange(HttpRequest.GET("/2").accept(
        MediaType.TEXT_PLAIN));

    assertEquals(200, response.code());
    assertEquals("Hello, World!", response.body());
  }
}

据我了解,响应主体永远不应该为空,但它是用于测试的shouldReturnHelloWorld2-shouldReturnHelloWorld1_1所以在使用时它总是为空HttpClient.exchange()。在我看来,这似乎是错误还是这里有什么问题?

您可以通过克隆我的示例存储库来检查整个代码并自己运行测试:https ://github.com/tobi6112/micronaut-httpclient-issue

更新: 刚刚注意到测试按预期工作

HttpResponse<String> response = client.toBlocking()
        .exchange(HttpRequest.GET("/2").accept(MediaType.TEXT_PLAIN), String.class);
4

0 回答 0