Reputation: 2346
In my Micronaut app I have a simple REST controller:
public class Response {
private String code;
public Response(String code) {
this.code = code;
}
}
@Controller("/api/test")
public class TestController {
@Post("/")
public Response index() {
return new Response("OK");
}
}
How can I tests this edpoint? I tried using
@MicronautTest
public class TestControllerTest {
@Inject
EmbeddedServer server;
@Inject
@Client("/")
HttpClient client;
@Test
void testResponse() {
String response = client.toBlocking()
.retrieve(HttpRequest.POST("/api/test/")); // FIXME `HttpRequest.POST` requires body
assertEquals("{\"code\": \"OK\"}", response);
}
but HttpRequest.POST
requires an additional body
argument to be specified. In my case there is no body to be sent. (In the real code it is a request to initialize a new object and thus it has to be POST).
Upvotes: 3
Views: 4128
Reputation: 42204
Usually, when you implement a POST action, you expect that there is a body sent with the request. In your example, you don't accept any POST body, but you still need to pass anything in the unit test.
You can instantiate the HttpRequest
object in the following way:
HttpRequest.POST("/api/test/", "");
You can't pass null
, it has to be some non-null value (like an empty string.)
Upvotes: 4