0

I have to test Jersey 1.19 with jersey-test-framework-grizzly2. There is configuration class with registered REST endpoint and exception mapper class:

public class ConfiguredMyServiceTest extends JerseyTest {

    @Override
    protected int getPort(int defaultPort) {
        return 8080;
    }

    public static class AppConfig extends DefaultResourceConfig {   
        public AppConfig() {
            getSingletons().add(new ExceptionMapperProvider());
            getSingletons().add(new MyService());
        }
    }

    @Override
    public WebAppDescriptor configure() {
        return new WebAppDescriptor.Builder()
            .initParam(WebComponent.RESOURCE_CONFIG_CLASS, 
                AppConfig.class.getName())
                .build();
    }
}

When I execute/test REST endpoint which returns HTTP status 200 it works well.

If exception is thrown, exception mapper handles it well and forms return object javax.ws.rs.core.Response with error code:

@Provider
@Singleton
public class ExceptionMapperProvider implements ExceptionMapper<Exception>{

    @Override
    public Response toResponse(final Exception exception){
        return Response.status(HttpStatusCodes.STATUS_CODE_SERVER_ERROR).entity(new BasicResponse(InternalStatus.UNHANDLED_EXCEPTION, exception.getMessage())).type(MediaType.APPLICATION_JSON).build();
    }
}

However, I get

com.sun.jersey.api.client.UniformInterfaceException: POST http://localhost:8080/v1/my-service/ returned a response status of 401 Unauthorized

when I try to assert Response in my JUnit tests. How to get well formed response instead of UniformInterfaceException?

Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
  • Do you have any authentication handlers in place because 401 means that you are not able to authenticate? Although I don't know what your constant STATUS_CODE_SERVER_ERROR evaluates to. Is this actually a 401? – Chris Hinshaw Jul 26 '16 at 19:38
  • STATUS_CODE_SERVER_ERROR means 500. I have simplified ExceptionMapper for SO so some parts were removed. However, it does not matter if its 401, 500 or other error code, the problem is the same - when I test, I get UniformInterfaceException instead of Response. – Justinas Jakavonis Jul 27 '16 at 06:31
  • 2
    I would recommend enabling logging filter so that you can see the request/response. Check here if you have never used it. Do you see the request hitting your rest service? Here is an example of enabling logging filter on the client side. http://stackoverflow.com/questions/2332515/how-to-get-jersey-logs-at-server You also may want to set the boolean printentity to true in the LoggingFilter constructor. This will verify that the 401 is actually coming from the server and not an issue with the client configuration. – Chris Hinshaw Jul 27 '16 at 20:08
  • I have put breakpoints in my endpoint and exception mapper code so both are hit/executed. – Justinas Jakavonis Jul 28 '16 at 06:45

1 Answers1

0

Changed expected class type to com.sun.jersey.api.client.ClientResponse

protected ClientResponse executeGet(String path){
    WebResource resource = resource().path(path);
    Builder builder = resource.header("Content-Type", "application/json;charset=UTF-8");
    return builder.get(ClientResponse.class);
}

Now it is able to handle various HTTP statuses and parse underlying response:

ClientResponse clientResponse = executeGet("/info");

if (clientResponse.getStatus() == 200)
    CustomResponseType customResponse = clientResponse.getEntity(CustomResponseType.class);
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114