Testing 404 Responses With Rest Client for MicroProfile

A 404 response:


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response greetingsNotFound() {
        return Response
                .ok("Greetings not found...")
                .status(404)
                .build();
    }
}

...accessed by MicroProfile Rest Client:


import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

@Path("hello")
@RegisterRestClient(baseUri = "http://localhost:8080")
public interface GreetingResourceClient{

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public Response greetingsNotFound();

}

...in the following test:


import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import io.quarkus.test.junit.QuarkusTest;

import javax.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;


@QuarkusTest
public class GreetingResourceIT {

    @Inject
    @RestClient
    GreetingResourceClient client;

    @Test
    public void test404(){
        var response = this.client.greetingsNotFound();
        assertNotNull(response);
        var status = response.getStatus();
        assertEquals(404,status);
        var message = response.readEntity(String.class);
        assertEquals("Greetings not found...",message);
    }
}

...throws the following exception:


javax.ws.rs.WebApplicationException: Unknown error, status code 404
    at airhacks.GreetingResourceIT.test404(GreetingResourceIT.java:21)

The exception can be suppressed by disabling the Default ResponseExceptionMapper with the following property:


microprofile.rest.client.disable.default.mapper=true

With the above configuration the test passes.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments
License