@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class UserResourceTest {
@Autowired RestTemplate restTemplate;
@BeforeClass
static public void setUp() throws Exception {
Server server = new Server(8080);
ServletContextHandler resource = new ServletContextHandler(ServletContextHandler.SESSIONS);
resource.setContextPath("/");
resource.getInitParams().put("contextConfigLocation", "classpath:applicationContext.xml");
ServletHolder servletDef = new ServletHolder(SpringServlet.class);
servletDef.setInitParameter("com.sun.jersey.config.property.packages","org.codehaus.jackson.jaxrs");
resource.addServlet(servletDef, "/*");
resource.addEventListener(new ContextLoaderListener());
resource.addEventListener(new RequestContextListener());
server.setHandler(resource);
server.setStopAtShutdown(true);
server.start();
}
@Test
public void test_rest_crud() throws Exception {
Object users = restTemplate.getForObject("http://localhost:8080/users", Object.class);
Assert.assertNotNull(users);
}
}
18/03/2012
Spring + JUnit + RestTemplate + Jetty + Jersey
After a whole day spent struggling, finally I managed to make it work. Now I can easily test my rest services.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment