-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3509 from loicmathieu/feat/test-resteasy-custom-e…
…xception-mapper feat: test custom exception mapper with resteasy
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...easy/deployment/src/test/java/io/quarkus/resteasy/test/CustomExceptionMapperTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
...sy/deployment/src/test/java/io/quarkus/resteasy/test/NotFoundExceptionMapperTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("/")); | ||
} | ||
|
||
} |