Skip to content

Commit

Permalink
Implement test for custom exception mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
AnetaCadova committed Aug 27, 2023
1 parent 543e44b commit 3316790
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import io.vertx.core.Vertx;
import org.apache.camel.CamelContext;
Expand Down Expand Up @@ -60,4 +63,31 @@ public boolean quarkusVertxInstanceUsedInVertxComponent() {
VertxComponent component = camelContext.getComponent("vertx", VertxComponent.class);
return component.getVertx() == vertx;
}

@Path("/exception")
@GET
@Produces("text/html")
public String exception() {
throw new RuntimeException("My runtime exception");
}

@Provider
public static class CustomNotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException exception) {
return Response.status(404)
.entity("Custom Not Found exception")
.build();
}
}

@Provider
public static class CustomWebApplicationExceptionMapper implements ExceptionMapper<Exception> {
@Override
public Response toResponse(Exception exception) {
return Response.status(400)
.entity("Custom exception")
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

@QuarkusTest
Expand All @@ -45,4 +46,22 @@ public void testVertxComponent() {
.statusCode(201)
.body(is("Hello " + message));
}

@Test
public void testDefaultExceptionMapper() {
RestAssured.given().accept("text/html")
.get("/vertx/exception")
.then()
.statusCode(400)
.body(containsString("Custom exception"));
}

@Test
public void testNotFoundExceptionMapper() {
RestAssured.given().accept("text/html")
.get("/vertx/exception2")
.then()
.statusCode(404)
.body(containsString("Custom Not Found exception"));
}
}

0 comments on commit 3316790

Please sign in to comment.