2009-03-03 88 views

回答

11

尝试SimpleMaven)它很容易嵌入到单元测试中。以RoundTripTest和例如用Simple编写的PostTest为例。提供如何将服务器嵌入到测试用例中的示例。

另外简单比Jetty更轻,更快,没有依赖关系。所以你不必在你的classpath中添加几个jar。您也不必关心WEB-INF/web.xml或任何其他工件。

+2

虽然它可能是用户想要的,但它不是一个“模拟”Web服务器,它是在单元测试中启动的实际Web服务器。例如,如果端口被占用,它将失败,所以不是真正的模拟(即,确实对系统有外部依赖)。根据马丁福勒的命名[“测试双打”](http://www.martinfowler.com/bliki/TestDouble.html),这是一个“假”。这就是说,它完全是**我正在寻找的东西。 – ArtB 2013-08-21 19:23:47

+0

这个项目失效了吗? – kbolino 2016-07-13 14:11:44

18

您是否在尝试使用mock or an embedded网络服务器?

对于模拟 Web服务器,请尝试使用Mockito,或类似的东西,只是嘲笑HttpServletRequestHttpServletResponse对象,如:

MyServlet servlet = new MyServlet(); 
HttpServletRequest mockRequest = mock(HttpServletRequest.class); 
HttpServletResponse mockResponse = mock(HttpServletResponse.class); 

StringWriter out = new StringWriter(); 
PrintWriter printOut = new PrintWriter(out); 
when(mockResponse.getWriter()).thenReturn(printOut); 

servlet.doGet(mockRequest, mockResponse); 

verify(mockResponse).setStatus(200); 
assertEquals("my content", out.toString()); 

对于嵌入式 web服务器,你可以使用Jetty,你可以在use in tests

6

另一个很好的选择将是MockServer;它提供了一个流畅的界面,您可以使用它来定义模拟Web服务器的行为。

+0

另外MockServer依赖于Netty(不是Jetty)。 – 2017-10-17 18:39:16

19

Wire Mock似乎提供了一套可靠的存根和嘲笑测试外部Web服务。

@Rule 
public WireMockRule wireMockRule = new WireMockRule(8089); 


@Test 
public void exactUrlOnly() { 
    stubFor(get(urlEqualTo("/some/thing")) 
      .willReturn(aResponse() 
       .withHeader("Content-Type", "text/plain") 
       .withBody("Hello world!"))); 

    assertThat(testClient.get("/some/thing").statusCode(), is(200)); 
    assertThat(testClient.get("/some/thing/else").statusCode(), is(404)); 
} 

它也可以与spock集成。例子发现here

+0

这对我来说非常好。使用wireMockConfig()。dynamicPort()使其在其他程序可能使用该端口的设置中更具可预测性。我希望它是默认的,因为我几乎错过了这种可能性,并跳过了这个伟大的图书馆。 – morten 2016-06-17 11:32:56

+0

你提供的代码中的testClient是什么? – gribo 2017-06-20 08:22:49

5

您可以尝试Jadler(http://jadler.net),它是一个带有流畅编程Java API的库,用于在测试中存根和模拟http资源。示例:

onRequest() 
    .havingMethodEqualTo("GET") 
    .havingPathEqualTo("/accounts/1") 
    .havingBody(isEmptyOrNullString()) 
    .havingHeaderEqualTo("Accept", "application/json") 
.respond() 
    .withDelay(2, SECONDS) 
    .withStatus(200) 
    .withBody("{\\"account\\":{\\"id\\" : 1}}") 
    .withEncoding(Charset.forName("UTF-8")) 
    .withContentType("application/json; charset=UTF-8"); 
4

您也可以使用JDK的HttpServer类编写模拟(不需要外部依赖关系)。详情请见this blog post

总结:

HttpServer httpServer = HttpServer.create(new InetSocketAddress(8000), 0); 
httpServer.createContext("/api/endpoint", new HttpHandler() { 
    public void handle(HttpExchange exchange) throws IOException { 
     byte[] response = "{\"success\": true}".getBytes(); 
     exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length); 
     exchange.getResponseBody().write(response); 
     exchange.close(); 
    } 
}); 
httpServer.start(); 

try { 
// Do your work... 
} finally { 
    httpServer.stop(0); 
} 
1

如果您使用的是Apache HttpClient的,这将是一个很好的选择。 HttpClientMock

HttpClientMock httpClientMock = new httpClientMock() 
HttpClientMock("http://example.com:8080"); 
httpClientMock.onGet("/login?user=john").doReturnJSON("{permission:1}"); 

全面披露:我是库的作者。