Testing With MicroProfile REST Client: How To Prevent WebApplicationException 📎
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
@Path("/SHOULD_NOT_EXIST")
@RegisterRestClient(baseUri = "https://microprofile.io/")
public interface ProblematicResourceClient {
@GET
@Produces(MediaType.TEXT_PLAIN)
Response fetchNotExistingContent();
}
will cause an WebApplicationException like e.g.:
javax.ws.rs.WebApplicationException: Unknown error, status code 404
at org.jboss.resteasy.microprofile.client.DefaultResponseExceptionMapper.toThrowable(DefaultResponseExceptionMapper.java:21)
(...)
To test the 404 status code:
import io.quarkus.test.junit.QuarkusTest;
import static org.junit.jupiter.api.Assertions.assertEquals;
import javax.inject.Inject;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.junit.jupiter.api.Test;
@QuarkusTest
public class ProblematicResourceClientIT {
@Inject
@RestClient
ProblematicResourceClient cut;
@Test
public void callNotExistingResource() {
var response = this.cut.fetchNotExistingContent();
assertEquals(response.getStatus(), 404);
}
}
...you need to set the MP config parameter to true
::
microprofile.rest.client.disable.default.mapper=true
Now the Response contains the status code 404
and the test passes.