Search code examples
javaeclipsejunitjerseyresteasy

Eclipse: JerseyTest.getClient returns RestEasyClient


I got a project where the jersey-client and the resteasyclient library is included. This has historic reasons, and since I had to include the resteasyclient , my junit-tests return at jersey-test.getClient() the resteasyclient and I am getting "RESTEASY004655 Unable to invoke the request", when executing the target-method. When I am manually excluding the resteasyclient.jar, it works.

Is there a way to exclude libraries while testing in eclipse?


Solution

  • Yeah it's crazy. This is just how the JAX-RS ClientBuilder is designed1; other ClientBuilders on the classpath take precedence over the Jersey ClientBuilder. This is only the case when you use the standard JAX-RS ClientBuilder, which is what the JerseyTest does.

    But each implementation comes with their own ClientBuilder implementation. For instance, Jersey comes with JerseyClientBuilder. If you want to use that then you can.

    Client client = JerseyClientBuilder.createClient();
    if (client instance JerseyClient) {
       System.out.println("Hip hip hooray!");
    }
    

    When using the JerseyTest, it gives you the ability to override the default client that is used.

    @Override
    public Client getClient() {
        return JerseyClientBuilder.createClient();
    }
    

    A couple things to note about this:

    1. If you are overriding configureClient in the JerseyTest, it won't work. You will just need to configure the client in the getClient() method.

    2. Probably the most important is that it won't work with the in-memory test provider. When using the in-memory test provider, the client is configured with a special in-memory connector. If you want to override the client, then you should use a "real server" test provider, like the grizzly2 one.


    1 - If you want to get into the gory details, checkout the source for ClientBuilder and the FactoryFinder that it uses.