Hot questions for Using Mockito in jersey test framework

Top 10 Java Open Source / Mockito / jersey test framework


I have REST web service which I test with Jersey Test, Mockito, Junit. When web service method is executed successfully, I get correct response. In case of invalid data, custom exception is thrown which must be handled by ExceptionMapper. It should return the same structure response but with different codes. ExceptionMapper works well in not test environment. However, after test execution logs show:

1 < 500
1 < Connection: close
1 < Content-Length: 1033
1 < Content-Type: text/html;charset=ISO-8859-1
1 < Date: Wed, 30 Mar 2016 11:55:37 GMT HTTP 500 Request failed.
at org.glassfish.jersey.client.JerseyInvocation.convertToException(
at org.glassfish.jersey.client.JerseyInvocation.translate(
at org.glassfish.jersey.client.JerseyInvocation.access$700(

I use:


Shortened version of mocked service which is extented by test classes:

public class MockedService extends JerseyTest {

    public ResourceConfig configure() {

        forceSet(TestProperties.CONTAINER_PORT, "8080");

        return new ResourceConfig().register(new Service());

How to get response from ExceptionMapper?


You still need to register the ExceptionMapper

return new ResourceConfig()
        .register(new Service())
        .register(new YourMapper());

In you real environment, the mapper is probably scanned for, either with package scanning or classpath scanning.


I have a file upload REST endpoint implemented in Dropwizard. I am new to this and just trying to learn.

public class FileUploadResource {

    private final MyAppWebConfiguration configuration;
    private static final Logger logger = LoggerFactory.getLogger(FileUploadResource.class);

    public FileUploadResource(MyAppWebConfiguration configuration) {
        this.configuration = configuration;

    public Response uploadFile(
            @FormDataParam("file") InputStream uploadedInputStream,
            @FormDataParam("file") FormDataContentDisposition fileDetail) throws IOException {"Request to upload the file ", fileDetail.getFileName());
        final String uploadedFileLocation = configuration.getCsvUploadPath();
        final String fileName = fileDetail.getFileName();
        writeToFile(uploadedInputStream, uploadedFileLocation, fileName);
        return Response.ok("File " + fileName + " is uploaded to the location " + uploadedFileLocation).build();

    // save uploaded file to new location
    protected void writeToFile(InputStream uploadedInputStream, String uploadedFileLocation, String fileName) throws IOException {"Writing {} to {}", fileName, uploadedFileLocation);
        final java.nio.file.Path outputPath = FileSystems.getDefault().getPath(uploadedFileLocation, fileName);
        Files.copy(uploadedInputStream, outputPath, StandardCopyOption.REPLACE_EXISTING);"Uploaded {} to the location {}", fileName, uploadedFileLocation);

The code works fine and am able to upload the file. I am trying to test it with below code based on :

public class FileUploadResourceTest extends JerseyTest {

    private final static MyAppWebConfiguration mockConfiguration = mock(MyAppWebConfiguration.class);

    public ResourceConfig configure() {
        return new ResourceConfig(FileUploadResource.class)
                .register(new LoggingFilter(Logger.getAnonymousLogger(), true));

    public void configureClient(ClientConfig config) {

    public void test() {
        FileDataBodyPart filePart = new FileDataBodyPart("file", new File("/Users/rocky/Downloads/test.csv"));

        MultiPart multiPart = new FormDataMultiPart()
        Response response = target("/files").request()
                .post(Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE));

This test fails with the below error:

WARNING: The following warnings have been detected: WARNING: HK2 service reification failed for [] with an exception:
MultiException stack 1 of 2
java.lang.NoSuchMethodException: Could not find a suitable constructor in class.
    at org.glassfish.jersey.internal.inject.JerseyClassAnalyzer.getConstructor(
    at org.jvnet.hk2.internal.Utilities.getConstructor(
    at org.jvnet.hk2.internal.ClazzCreator.initialize(
    at org.jvnet.hk2.internal.ClazzCreator.initialize(

I do not have a no argument constructor in FileUploadResource but new ResourceConfig(FileUploadResource.class) expects a no argument constructor. How do I pass the info about the one argument constructor here?

Any help here would be highly appreciated. Also, please feel free to suggest any other best practices about the code and the tests so that I can improve them.

Many thanks in advance.


When you register the resource as a class

new ResourceConfig(FileUploadResource.class)

You are telling Jersey to create it. But it has no idea how to create it, as there is only a constructor that accepts a configuration object, which Jersey knows nothing about. Instead you should just register as an object. Same way as if you were registering with Dropwizard (env.jersey().register(...)).

new ResourceConfig().regster(new FileUploadResource(mockConfiguration))

As an aside, with Dropwizard we don't need to explicitly use the JerseyTest. Dropwizard comes with a JUnit Rule, that explicitly run its own JerseyTest, and we can configure it with the rule. See this issue, where I posted a complete example.