6

I have setup a simple test Controller:

@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!");
  }
}

And I am using the low-level HTTPClient in my Unit-Tests, which looks like this:

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

From my understanding the response body should never be null, however it is for the tests shouldReturnHelloWorld2 and shouldReturnHelloWorld1_1 - so it is always null when HttpClient.exchange() is used. In my opinion this seems to be bug or is here any issue?

You can check the whole code and run the tests yourself by cloning my sample repository: https://github.com/tobi6112/micronaut-httpclient-issue

Update: Just noticed that the tests work as expected with

HttpResponse<String> response = client.toBlocking()
        .exchange(HttpRequest.GET("/2").accept(MediaType.TEXT_PLAIN), String.class);
twobiers
  • 1,057
  • 1
  • 11
  • 27
  • i suggest checking with a breakpoint if the dedicated controller functions are called as expected, maybe that could help you – IEE1394 May 23 '21 at 12:39
  • 1
    "From my understanding the response body should never be null" - I don't think that is a correct understanding. The docs at https://docs.micronaut.io/2.5.4/api/io/micronaut/http/HttpResponse.html#body-- say that the method returns "The body or null". – Jeff Scott Brown May 24 '21 at 17:44
  • The edit that you made to the question should allow the `.body()` method to return non-null. – Jeff Scott Brown May 24 '21 at 17:45
  • @JeffScottBrown Yes that work's perfectly fine. However, according to the docs of the BlockingHttpClient https://docs.micronaut.io/2.1.3/api/io/micronaut/http/client/BlockingHttpClient.html#exchange-io.micronaut.http.HttpRequest- the parameter should not be required as it can be inferred from the generic type of `HttpResponse`. Maybe I'm wrong but this is my expectation from the docs. – twobiers May 24 '21 at 17:58

1 Answers1

0

In my case these two options work:

final var result = client.toBlocking().exchange(HttpRequest.GET(url).accept(MediaType.APPLICATION_JSON), String.class);

HttpResponse<String> response  = client.toBlocking().exchange(HttpRequest.GET(url).accept(MediaType.APPLICATION_JSON), String.class);
David AJ
  • 29
  • 1
  • 4