它可以用在哪些场景下:
测试移动应用依赖于第三方REST APIs
创建快速原型的APIs
注入否则难于模拟第三方服务中的错误
任何单元测试的代码依赖于web服务的
目录 前提条件 Maven配置 准备工作 Examples Troubleshooting 参考 |
前提条件
JDK 1.7
Maven 3
Maven配置
pom里添加以下的dependencies
<dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>1.53</version> <classifier>standalone</classifier> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8</version> </dependency> |
如果有依赖冲突,可以exclued 掉冲突的依赖, 配置如下
<dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>1.53</version> <!-- Include everything below here if you have dependency conflicts --> <classifier>standalone</classifier> <exclusions> <exclusion> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty</artifactId> </exclusion> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </exclusion> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </exclusion> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </exclusion> <exclusion> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </exclusion> <exclusion> <groupId>org.skyscreamer</groupId> <artifactId>jsonassert</artifactId> </exclusion> <exclusion> <groupId>xmlunit</groupId> <artifactId>xmlunit</artifactId> </exclusion> <exclusion> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> </exclusion> <exclusion> <groupId>net.sf.jopt-simple</groupId> <artifactId>jopt-simple</artifactId> </exclusion> </exclusions> </dependency> |
准备工作
首先我写了一个类HTTPRequestor用来执行Http request访问Rest服务的, 然后我需要一个Rest服务来测试我写的类是否ok, 但我手上没有一个真实的Rest web service, 所以WireMock就可以出场了,模拟一个Rest web serivce来测试我这个类。
HTTPRequestor如下:
1 package com.demo.HttpRequestor; 2 3 import static com.jayway.restassured.RestAssured.given; 4 5 import java.util.HashMap; 6 import java.util.Map; 7 8 import org.slf4j.Logger; 9 import org.slf4j.LoggerFactory; 10 11 import com.jayway.restassured.response.Response; 12 import com.jayway.restassured.specification.RequestSpecification; 13 14 /** 15 * Wrapper for RestAssured. Perform an HTTP requests. 16 * 17 * @author wadexu 18 * 19 */ 20 public class HTTPRequestor { 21 22 protected static final Logger logger = LoggerFactory.getLogger(HTTPRequestor.class); 23 private RequestSpecification reqSpec; 24 25 26 /** 27 * Constructor. Initializes the RequestSpecification (relaxedHTTPSValidation 28 * avoids certificate errors). 29 * 30 */ 31 public HTTPRequestor() { 32 reqSpec = given().relaxedHTTPSValidation(); 33 } 34 35 public HTTPRequestor(String proxy) { 36 reqSpec = given().relaxedHTTPSValidation().proxy(proxy); 37 } 38 39 /** 40 * Performs the request using the stored request data and then returns the response 41 * 42 * @param url 43 * @param method 44 * @param headers 45 * @param body 46 * @return response Response, will contain entire response (response string and status code). 47 * @throws Exception 48 */ 49 public Response perform_request(String url, String method, HashMap<String, String> headers, String body) throws Exception { 50 51 Response response = null; 52 53 try { 54 55 for(Map.Entry<String, String> entry: headers.entrySet()) { 56 reqSpec.header(entry.getKey(), entry.getValue()); 57 } 58 59 switch(method) { 60 61 case "GET": { 62 response = reqSpec.get(url); 63 break; 64 } 65 case "POST": { 66 response = reqSpec.body(body).post(url); 67 break; 68 } 69 case "PUT": { 70 response = reqSpec.body(body).put(url); 71 break; 72 } 73 case "DELETE": { 74 response = reqSpec.delete(url); 75 break; 76 } 77 78 default: { 79 logger.error("Unknown call type: [" + method + "]"); 80 } 81 } 82 83 } catch (Exception e) { 84 logger.error("Problem performing request: ", e); 85 } 86 87 return response; 88 } 89 } |
这个类是需要依赖 jayway 的 rest-assured包的
<dependency> <groupId>com.jayway.restassured</groupId> <artifactId>rest-assured</artifactId> <version>2.3.3</version> <scope>test</scope> </dependency> |
上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。