Retrieve a managed bean from a JerseyTest container with jersey-spring3 Retrieve a managed bean from a JerseyTest container with jersey-spring3 spring spring

Retrieve a managed bean from a JerseyTest container with jersey-spring3


Note: I am not a Spring expert and I consider this to be rather a work-around than a recommended approach. Hopefully someone will come up with a better solution.

You can't obtain an ApplicationContext instance by calling ContextLoader#getCurrentWebApplicationContext() because Jersey 2.x runtime is by default initialized outside of a Servlet container when using Jersey Test Framework (JerseyTest) for unit/e2e tests.

In this case you need to use a little work-around to obtain an ApplicationContext by implementing an ApplicationContextAware interface in your test package:

public class ApplicationContextUtils implements ApplicationContextAware {    private static ApplicationContext applicationContext;    public static ApplicationContext getApplicationContext() {        return applicationContext;    }    @Override    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {        ApplicationContextUtils.applicationContext = applicationContext;    }}

Once you have this class, don't forget to mention it in your application context descriptor:

...<bean class="org.glassfish.jersey.examples.helloworld.spring.ApplicationContextUtils" />...

And you can use it in your tests:

public class JerseySpringResourceTest extends JerseyTest {    // ... Configure ...    @Before    public void mockUp() throws Exception {        // ApplicationContext is ready in your @Before methods ...        assertThat(ApplicationContextUtils.getApplicationContext(), notNullValue());    }    @Test    public void testJerseyResource() {        // ... as well as in your test methods.        assertThat(ApplicationContextUtils.getApplicationContext(), notNullValue());    }}

Note: If you want to deploy your application to a Servlet container and run your (JerseyTest) tests against it, consult Jersey Test Framework chapter in the Users Guide (especially External container section).


You can inject your test class to Jersey context if you don't have any objections for that.

For example:

@Overrideprotected Application configure() {    final TestJerseyApplication application = new TestJerseyApplication();    final Map<String, Object> properties = new HashMap<>();    properties.put("contextConfigLocation", "classpath:test-spring-context.xml");    application.setProperties(properties);    application.register(this);    return application;}

After that the @Autowired annotation will work for you.


For the Jersey 2.X users, here's what worked for me:

  public class AccountResourceTest extends JerseyTest {    private ApplicationContext context;    private BeanA beanA;    private BeanB beanB;    public AccountResourceTest() throws TestContainerException {        super();        beanA = context.getBean(BeanA.class);        beanB = context.getBean(BeanB.class);    }    @Override    protected Application configure() {        context = new AnnotationConfigApplicationContext(SpringConfiguration.class);        final ResourceConfig config = new JerseyConfiguration().property("contextConfig", context);        return config;    }    @Override    protected void configureClient(final ClientConfig config) {        config.register(JacksonJsonProvider.class);    }    ...}

This allows me to use JavaConfig for my Jersey tests, and access the beans in the context as well. Here's the link to where I got the idea: http://geowarin.github.io/spring-boot/jersey/2014/01/31/a-simple-spring-boot-and-jersey-application.html