Hi everybody,
I am trying to create a JerseyTest setup where a Spring applicationContext is externally provided to the JerseyTest.
I am doing this by overriding the 'configure' as well as the 'configureDeployment' methods of JerseyTest. I can see that the applicationContext is there and contains all the necessary beans. However, it is not used by h2k when injecting dependencies annotated
with the
@Inject annotation into my test resource. Instead, I get this Stacktrace:
ERROR: service exception: - MDC: {} (ServletHandler.java:234 - org.glassfish.grizzly.servlet.ServletHandler)
javax.servlet.ServletException: javax.servlet.ServletException: A MultiException has 3 exceptions. They are:
1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=DemoService,parent=DemoResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,860330597)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of webui2.rest.DemoResource errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on webui2.rest.DemoResource
In am using this configuration for my JerseyTest (for completeness' sake I am posting the whole class, which is a JUnit4 TestRule, but that should not be relevant. Configuration is in the initJerseyTest method):
class JerseyRule implements TestRule {
private JerseyTest _jerseyTest;
private final Supplier<ApplicationContext> _applicationContextSupplier;
JerseyRule(Supplier<ApplicationContext> applicationContextSupplier) { _applicationContextSupplier = applicationContextSupplier; }
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
_jerseyTest = initJerseyTest(description.getTestClass());
try {
base.evaluate();
} finally {
_jerseyTest.tearDown();
}
}
};
}
private JerseyTest initJerseyTest(Class<?> testClass) throws Exception {
JerseyTest jerseyTest = new JerseyTest() {
@Override
protected ResourceConfig configure() {
return new ResourceConfig()
.packages(testClass.getPackageName())
.property("contextConfig", _applicationContextSupplier.get());
}
@Override
protected TestContainerFactory getTestContainerFactory() {
return new GrizzlyWebTestContainerFactory();
}
@Override
public DeploymentContext configureDeployment() {
return ServletDeploymentContext
.forServlet(new ServletContainer(configure()))
.initParam("contextConfigLocation", "")
.build();
}
};
jerseyTest.setUp();
return jerseyTest;
}
WebTarget getWebTarget(String path) {
return _jerseyTest.target(path);
}
}
Any help is much appreciated!
|