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("com.some.package.name")
.filterClass(GuiceFilter.class)
.contextPath("jersey-ctx-path")
.servletPath("/")
.clientConfig(new DefaultClientConfig(JacksonJaxbJsonProvider.class))
.build();
public JerseyTestNG() {
super(APP_DESCRIPTOR);
}
@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new OnePerAppDescriptorTestContainerFactory(super.getTestContainerFactory());
}
@BeforeSuite
public void initTestContainer() throws Exception {
setUp();
}
@AfterSuite
public void tearDownTestContainer() throws Exception {
tearDown();
}
}
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;
}
@Override
public <T extends AppDescriptor> Class<T> supports() {
return tcf.supports();
}
@Override
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