Micronaut HttpClients 交换体始终为空

问题描述

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

@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,response);
  }

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

    assertEquals(200,response.body());
  }
}

根据我的理解,响应主体永远不应该为空,但它用于测试 shouldReturnHelloWorld2shouldReturnHelloWorld1_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);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)