Search code examples
javajunitresteasyjersey-clientwildfly-10

Using JUnit and Jersey Client for testing a JAX-RS application


I have a Java EE application running on WildFly 10. This application uses Jersey and is running fine when I test it using a REST client.

I wrote a JUnit test that uses the Jersey Client API to make a request to the above application. When I run it, I get the following:

javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.handleErrorStatus(ClientInvocation.java:209)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.extractResult(ClientInvocation.java:174)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocation.invoke(ClientInvocation.java:473)
    at org.jboss.resteasy.client.jaxrs.internal.ClientInvocationBuilder.get(ClientInvocationBuilder.java:165)

The next line in the trace references the request() call below:

@Test
public void test() {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8080/myapp/webapi");
    target.path("/users");
    String response = target.request("application/json").get(String.class);
    assertEquals("test", response);
}

Any ideas?


Solution

  • Your problem comes from the server side (see the error 500), if you want to check it yourself, open a browser and go to the url: http://localhost:8080/myapp/webapi

    Also notice from the Javadoc that WebTarget.path() returns a new WebTarget

    https://jersey.java.net/apidocs/2.22/jersey/javax/ws/rs/client/WebTarget.html#path(java.lang.String)

    I believe in your code what you actually want to do is:

    @Test
    public void test() {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target("http://localhost:8080/myapp/webapi");
        WebTarget targetUpdated = target.path("/users");
        String response = targetUpdated.request("application/json").get(String.class);
        assertEquals("test", response);
    }