Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Better JerseyTest
public class BetterJerseyTest extends JerseyTest {
//Fire up jersey with Guice
private static final AppDescriptor APP_DESCRIPTOR = new WebAppDescriptor.Builder("")
.clientConfig(new DefaultClientConfig(JacksonJaxbJsonProvider.class))
public JerseyTestNG() {
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new OnePerAppDescriptorTestContainerFactory(super.getTestContainerFactory());
public void initTestContainer() throws Exception {
public void tearDownTestContainer() throws Exception {
class OnePerAppDescriptorTestContainerFactory implements TestContainerFactory {
private static final ConcurrentMap<AppDescriptor, TestContainer> cache = new ConcurrentHashMap<AppDescriptor, TestContainer>();
private final TestContainerFactory tcf;
public OnePerAppDescriptorTestContainerFactory(TestContainerFactory tcf) {
this.tcf = tcf;
public <T extends AppDescriptor> Class<T> supports() {
return tcf.supports();
public TestContainer create(URI baseUri, AppDescriptor ad) {
if (cache.get(ad) == null) {
cache.putIfAbsent(ad, tcf.create(baseUri, ad));
return cache.get(ad);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment