I am using:
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<version>2.27</version>
For REST testing of my resources. While everything works well on the positive side, when my resource throws a custom exception it is always discarded, and a 500 exception is returned by the test container.
For example:
//Omitting all annotations for brevity
import org.rest.exception.NotFoundException;
public class MyResource {
public void myAPI() {
throw new NotFoundException("Alarm! Not Found!");
}
}
When I want to test it:
import javax.ws.rs.core.Response;
public class MyJerseyTest extends JerseyTest {
public void myTest() {
Response response = target("path...").request().get();
assertEquals(response.getStatus(), HttpStatus.SC_NOT_FOUND);
}
}
Actual response returned:
InboundJaxrsResponse{context=ClientResponse{method=GET, uri=http://localhost:9998/path, status=500, reason=Request failed.}}
How can I fix this in order to test the logic throwing expected exceptions by my resources?
After further investigation I realized my custom exception is mapped by an exception mapper:
I simply needed to register this mapper on
ResourceConfig
:And now it works as expected.