Skip to content

Commit

Permalink
Merge pull request #3509 from loicmathieu/feat/test-resteasy-custom-e…
Browse files Browse the repository at this point in the history
…xception-mapper

feat: test custom exception mapper with resteasy
  • Loading branch information
gsmet authored Aug 22, 2019
2 parents a6a1e7c + 1d6dbb2 commit cb29369
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
11 changes: 11 additions & 0 deletions extensions/resteasy/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<!-- These are needed to test the JSON representation of the NotFoundExceptionMapper-->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-json-binding-provider</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.resteasy.test;

import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class CustomExceptionMapperTestCase {
@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(RootResource.class, CustomExceptionMapper.class));

@Test
public void testResourceNotFound() {
RestAssured.when().get("/not_found")
.then()
.statusCode(200)
.body(Matchers.is("not found but OK"));
}

@Provider
public static class CustomExceptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException exception) {
return Response.status(200).entity("not found but OK").build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.quarkus.resteasy.test;

import static org.hamcrest.Matchers.is;

import org.hamcrest.Matchers;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class NotFoundExceptionMapperTestCase {
@RegisterExtension
static QuarkusDevModeTest test = new QuarkusDevModeTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(RootResource.class));

@Test
public void testHtmlResourceNotFound() {
// test the default exception mapper provided in dev mode : displays HTML by default
RestAssured.when().get("/not_found")
.then()
.statusCode(404)
.body(Matchers.containsString("<div class=\"component-name\"><h1>Resource Not Found</h1>"));
}

@Test
public void testJsonResourceNotFound() {
// test the default exception mapper provided in dev mode : displays json when accept is application/json
RestAssured.given().accept(ContentType.JSON)
.when().get("/not_found")
.then()
.statusCode(404)
.body("errorMessage", is("Resource Not Found"))
.body("existingResourcesDetails[0].basePath", is("/"));
}

}

0 comments on commit cb29369

Please sign in to comment.