I am working on a REST project implemented with Jersey 2 (for REST API) and Spring (for DI), and I want to write functional/integration tests. I tried to use JerseyTest framework and to use real database for those tests. Only thing that I have in my mind to mock are remote Web Services (SOAP) that my app consumes, for which I should mock generated WS clients.
After a lot of time spent on investigations about JerseyTest framework with Jersey2 and Spring it seems it is not possible to have that setup for integration tests. Can you tell me did you succeeded to setup something similar?
Problems with JerseyTest is AFAIK that I can not use all settings from web.xml config file, like registering multiple filters and servlets, and also that I can not use the same Spring context that I configured for app that runs under tests for defining mock objects and what they return per test. Each my REST resource is under Spring Security protection, it is also not loaded with JerseyTest framework because of the need to register it's listener.
Please give some advices how to achieve that or maybe to use another testing framework in order to achieve all this mentioned...
Here is my code from that junit test:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {MyResourceTest.MOCK_SPRING_APPLICATION_CONTEXT})
public class MyResourceTest extends JerseyTest {
public static final String MOCK_SPRING_APPLICATION_CONTEXT = "classpath:spring/testApplicationContext.xml";
@Override
protected Application configure() {
ResourceConfig resourceConfig = new MyResourceConfig();
disable(TestProperties.LOG_TRAFFIC);
disable(TestProperties.DUMP_ENTITY);
resourceConfig.property("contextConfigLocation", MOCK_SPRING_APPLICATION_CONTEXT);
return resourceConfig;
}
@Override
protected TestContainerFactory getTestContainerFactory() {
return new GrizzlyWebTestContainerFactory();
}
@Override
protected DeploymentContext configureDeployment() {
return ServletDeploymentContext
.forServlet(new ServletContainer(new MyResourceConfig()))
.addListener(MyContextLoaderListener.class) // Extends Spring's listener
.addListener(RequestContextListener.class)
.contextParam("contextConfigLocation", MOCK_SPRING_APPLICATION_CONTEXT)
.build();
}
@Test
@SqlGroup({
@Sql(scripts = {"classpath:db_scripts/clean-up.sql", "classpath:db_scripts/init-db.sql"}),
@Sql(scripts = "classpath:db_scripts/clean-up.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD)
})
public void shouldReceiveResourceTest() throws IOException {
// Prepare request ...
final MyResponse myResponse = target("/resource/1").request().post(myRequest, MyResponse.class);
assertNotNull(myResponse);
}
}