X

Pavel Bucek's Weblog

  • February 11, 2011

Replacing client used in Jersey Test Framework

There was an interesting question on mailing list - Is it possible to replace default Jersey client in JerseyTest? (JerseyTest is a class from Jersey Test Framework, basically superclass of all "Jersey enabled" tests).

Answer is.. not really. It just wasn't made to support this option. Until now :)

There was method getClient(), but it couldn't be easily overriden (it was private static) and it actually does more that create Client, so it isn't really method you want to replace. Long story short.. I created overridable ClientFactory which is (surprisingly) responsible for creating new Clients..

Let's say we want to just replace default Jersey Client implementation by Apache:

public class MainTest extends JerseyTest {
    ...
    @Override
    protected ClientFactory getClientFactory() {
        return new ClientFactory() {
            @Override
            public Client create(ClientConfig clientConfig) {
                return ApacheHttpClient.create(clientConfig);
            }
        };
    }
}

This overrides default client implementation BUT still allows container override this and use its client (this is used for example in InMemoryTestContainer). If you want to have total control and replace even this concept, you can do it by overriding getClient() method:

    @Override
protected Client getClient(TestContainer tc, AppDescriptor ad) {
return ApacheHttpClient.create(ad.getClientConfig());
}

but I don't recommend this approach, former one should be sufficient in most cases.

This functionality is present in 1.6-SNAPSHOT and 1.6-ea03 (and newer Jersey versions).

Join the discussion

Comments ( 1 )
  • Pulkit Singhal Wednesday, October 23, 2013

    Replacing the client is pretty useful! Thanks :)

    And if someone wants to only change the config then instead of replacing the client, there's an easy way to do so: https://java.net/projects/jersey/lists/users/archive/2011-07/message/43

    @Override

    protected AppDescriptor configure() {

    ...

    return new WebAppDescriptor.Builder("com.mypackage")

    .clientConfig(<your config here>)

    .build();

    }


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha
Oracle

Integrated Cloud Applications & Platform Services