springtestingjerseyjax-rsgrizzly

In Jersey test (with Grizzly), how to use Spring Java-based configuration to set up the test context?


I am using JAX-RS, Jersey and Spring.

I have this integration test for my JAX-RS resource.

public class MyResourceIT extends JerseyTest {

    @Override
    protected Application configure() {
        return new ResourceConfig(MyResource.class);
    }

    @Test
    public void getHealth() {
      // some code
    }
}

When I start the test, I get this error:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:342)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310)
    at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:196)

I could create an applicationContext.xml file (see https://stackoverflow.com/a/33323141/3067148), but I would rather use Spring Java-base container configuration to set up my test context.

How can I tell the JerseyTest to use a Java Spring context configuration?


Solution

  • It worked this way:

    public class MyResourceTest extends JerseyTest {
    
        @Override
        protected Application configure() {
            var resourceConfig = new ResourceConfig(MyResource.class);
            resourceConfig.property(
                    "contextConfig", 
                    new AnnotationConfigApplicationContext(MySpringTestConfig.class)
            );
            return resourceConfig;
        }
    
    }