我需要通过http向来自Jetty的请求客户端发送存根响应。当我独立运行Junit测试时,它起作用,暗示,我得到了正确的XML响应..但是当我从maven运行相同的东西时它失败了。我看到的错误是“java.net.SocketException: Unexpected end of file from server
”。我已经尝试了一切!请帮忙!
这是我的代码 -
public class MyTest {
@Test
public void testGetOpenLots() throws Exception {
// create fixture
MyService fixture = new MyService();
// create jetty server instance
Server server = new Server(8080);
// set a handler
server.setHandler(new HelloHandler());
// set shutdown conditions
// server.setStopAtShutdown(true);
// start server
server.start();
// invoke operation
MyResponse result = fixture.getWeather(someDummyRequest);
assertNotNull(result);
}
}
在某个地方,在getWeather()内部,我创建了一个URL对象并将URL http://localhost:8080
传递给它并将请求发送到该URL。此时,我希望HelloHandler的handle方法将被调用,并将这个虚拟XML响应写入stream,getWeather()方法将收到响应。
public class HelloHandler extends AbstractHandler {
public void handle(String target, Request baseRequest,
HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("application/xml;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("<result>a simple response</result>");
}
}
当我从maven运行相同的东西时,它会抛出上面提到的错误。我做错了什么?