Skip to content

Commit 9551ec5

Browse files
committed
使用WireMock实现Http Server Mock作单元测试
1 parent d3af7c5 commit 9551ec5

1 file changed

Lines changed: 131 additions & 0 deletions

File tree

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
使用WireMock实现Http Server Mock作单元测试
2+
===
3+
前一篇文章"[使用jetty实现Http Server Mock作单元测试](http://aofengblog.blog.163.com/blog/static/631702120144581541484/)"
4+
讲了用Jetty实现Http Server Mock来模拟依赖的外部HTTP服务系统,但如果需要更多的功能,如:区分GET和POST;匹配请求的URL;匹配Http Header;匹配请求内容等等。
5+
实际研发的过程中这些功能都有可能会用到,如果还是用Jetty来实现,需要自己不停地动手去添砖加瓦,虽然有成就感,但在快速迭代的节奏下不一定有足够的时间去做这些。
6+
这时我们需要一个现成的类库来满足我们这些需求,WireMock和MockServer都可以做到,这里只讲WireMock。
7+
8+
预备
9+
---
10+
* [JUnit 4.11](http://junit.org/)
11+
* [wiremock 1.46](http://wiremock.org/)
12+
13+
业务示例代码
14+
---
15+
[源码下载](https://raw.githubusercontent.com/aofeng/JavaDemo/master/src/cn/aofeng/demo/jetty/HttpGet.java)
16+
```java
17+
package cn.aofeng.demo.jetty;
18+
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.net.HttpURLConnection;
23+
import java.net.URL;
24+
25+
import org.apache.commons.io.IOUtils;
26+
27+
/**
28+
* 抓取页面内容。
29+
*
30+
* @author <a href="mailto:[email protected]">聂勇</a>
31+
*/
32+
public class HttpGet {
33+
34+
public String getSomeThing(String urlStr) throws IOException {
35+
URL url = new URL(urlStr);
36+
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
37+
urlConn.setConnectTimeout(3000);
38+
urlConn.setRequestMethod("GET");
39+
urlConn.connect();
40+
41+
InputStream ins = null;
42+
try {
43+
if (200 == urlConn.getResponseCode()) {
44+
ins = urlConn.getInputStream();
45+
ByteArrayOutputStream outs = new ByteArrayOutputStream(1024);
46+
IOUtils.copy(ins, outs);
47+
return outs.toString("UTF-8");
48+
}
49+
} catch (IOException e) {
50+
throw e;
51+
} finally {
52+
IOUtils.closeQuietly(ins);
53+
}
54+
55+
return null;
56+
}
57+
58+
}
59+
```
60+
61+
单元测试代码
62+
---
63+
[源码下载](https://raw.githubusercontent.com/aofeng/JavaDemo/master/src/cn/aofeng/demo/wiremock/HttpGetTest.java)
64+
```java
65+
package cn.aofeng.demo.wiremock;
66+
67+
import static org.junit.Assert.*;
68+
69+
import org.junit.After;
70+
import org.junit.Before;
71+
import org.junit.Rule;
72+
import org.junit.Test;
73+
74+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
75+
import com.github.tomakehurst.wiremock.junit.WireMockRule;
76+
77+
import cn.aofeng.demo.jetty.HttpGet;
78+
79+
/**
80+
* {@link HttpGet}的单元测试用例。
81+
*
82+
* @author <a href="mailto:[email protected]">聂勇</a>
83+
*/
84+
public class HttpGetTest {
85+
86+
private HttpGet _httpGet = new HttpGet();
87+
88+
@Rule
89+
public WireMockRule _wireMockRule = new WireMockRule(9191);
90+
91+
@Before
92+
public void setUp() throws Exception {
93+
}
94+
95+
@After
96+
public void tearDown() throws Exception {
97+
}
98+
99+
/**
100+
* 用例:响应状态码为200且有响应内容。
101+
*/
102+
@Test
103+
public void testGetSomeThing4Success() throws Exception {
104+
// 设置Mock
105+
String response = "Hello, The World!";
106+
stubFor(get(urlEqualTo("/hello"))
107+
.willReturn(aResponse()
108+
.withStatus(200)
109+
.withBody(response)));
110+
111+
String content = _httpGet.getSomeThing("http://localhost:9191/hello");
112+
assertEquals(response, content);
113+
}
114+
115+
/**
116+
* 用例:响应状态码为非200。
117+
*/
118+
@Test
119+
public void testGetSomeThing4Fail() throws Exception {
120+
// 设置Mock
121+
stubFor(get(urlEqualTo("/hello"))
122+
.willReturn(aResponse()
123+
.withStatus(500)
124+
.withBody("Hello, The World!")));
125+
126+
String content = _httpGet.getSomeThing("http://localhost:9191/hello");
127+
assertNull(content);
128+
}
129+
130+
}
131+
```

0 commit comments

Comments
 (0)