users@jersey.java.net

[Jersey] JerseyTest and Spring and creating a ServletContext

From: Raishad Maharaj <rincewind1013_at_gmail.com>
Date: Wed, 26 Mar 2014 16:12:22 -0500

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?