我想要一些在 Jetty 9 中运行 Jersey 2.2 的 JUnit 测试。这就是我所拥有的:
就业服务
...
@Path("jobs")
public JobService() {
@GET
public Response list(){
return Response.ok("Hello World!").build();
}
}
我的 JUnit 测试中的 jetty 嵌入式服务器代码
@Before
public void startServer() throws MalformedURLException, IOException {
_server = new Server();
_connector = new LocalConnector(_server);
_server.setConnectors(new Connector[]{_connector});
// Prepare rest service servlet
ServletHolder _jersey = new ServletHolder(new ServletContainer(new ResourceConfig(JobService.class, UserService.class)));
_jersey.setName("restful service servlet");
ServletContextHandler _servletCtx = new ServletContextHandler();
_servletCtx.addServlet(_jersey, "/rest/*");
_servletCtx.setContextPath("/ctx");
_server.setHandler(_servletCtx);
}
@Test
public void testServer() throws Exception{
_server.start();
String response = null;
response = _connector.getResponses("GET /ctx/rest/users HTTP/1.0\r\n");
assertThat(response,startsWith("HTTP/1.1 200 "));
assertThat(response,containsString("[]"));
}
Jersey 不发送响应,在 Eclipse 中运行它时,我在控制台中也没有异常。
我错过了什么?