Discussion:
JerseyTest and Spring and creating a ServletContext
Raishad Maharaj
2014-03-26 21:12:22 UTC
Permalink
I'm working on migrating from Jersey 1.16 to Jersey 2.7. I have the
application running and working, but I'm having trouble with the tests.

Some things to note:
The application uses Spring and is configured by a class,
ApplicationConfiguration.class.
The application does not have a web.xml - the Jersey servlet and filters
are configured programmatically.
I am using the jersey-spring3 library.
Related to using the jersey-spring3 library, I have to add a workaround to
my onStartup method

// The following line is required to avoid having jersey-spring3
registering it's own Spring root context.
// https://java.net/jira/browse/JERSEY-2038
servletContext.setInitParameter("contextConfigLocation", "");

Here's the issue:
When the test is starting up, SpringComponentProvider, tries to initialize
Spring with a dangerous assumption that I can't figure out how to correct -
xml based configuration. Looking at the code, the trouble is this block

ServletContext sc = locator.getService(ServletContext.class);

if(sc != null) {
// servlet container
ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
} else {
// non-servlet container
ctx = createSpringContext();
}

Running a JUnit test, ServletContext is null, and createSpringContext is
called.

Here's the question:
Is there a way to run a test and specify a ServletContext?
cowwoc
2014-03-27 02:01:54 UTC
Permalink
HI Raishad,

I believe this is covered by https://java.net/jira/browse/JERSEY-2259

Gili
Post by Raishad Maharaj
I'm working on migrating from Jersey 1.16 to Jersey 2.7. I have the
application running and working, but I'm having trouble with the tests.
The application uses Spring and is configured by a class,
ApplicationConfiguration.class.
The application does not have a web.xml - the Jersey servlet and
filters are configured programmatically.
I am using the jersey-spring3 library.
Related to using the jersey-spring3 library, I have to add a
workaround to my onStartup method
// The following line is required to avoid having
jersey-spring3 registering it's own Spring root context.
// https://java.net/jira/browse/JERSEY-2038
servletContext.setInitParameter("contextConfigLocation", "");
When the test is starting up, SpringComponentProvider, tries to
initialize Spring with a dangerous assumption that I can't figure out
how to correct - xml based configuration. Looking at the code, the
trouble is this block
ServletContext sc = locator.getService(ServletContext.class);
if(sc != null) {
// servlet container
ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
} else {
// non-servlet container
ctx = createSpringContext();
}
Running a JUnit test, ServletContext is null, and createSpringContext
is called.
Is there a way to run a test and specify a ServletContext?
Loading...