Jetty-实现Http Server Mock用于单元测试

作者:聂勇 欢迎转载,请保留作者信息并说明文章来源!
木桥_大海

在研发过程中发现许多模块对外部系统的调用并没有沉淀成专门的客户端,代码中直接调用URL发送请求和获取响应内容,导致做单元测试的时候非常麻烦。 特别是对已有系统做改造的时候,要先写单元测试用例,保证后续的重构和修改不偏离原来的业务需求。

这样问题就来了:该如何模拟外部系统返回各种正常和异常的响应内容呢? 办法总是有的,这里就用Jetty实现Http Server Mock,模拟服务端返回各种响应数据。

预备

业务示例代码

源码下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cn.aofeng.demo.jetty;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.io.IOUtils;
/**
* 抓取页面内容。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class HttpGet {
public String getSomeThing(String urlStr) throws IOException {
URL url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(3000);
urlConn.setRequestMethod("GET");
urlConn.connect();
InputStream ins = null;
try {
if (200 == urlConn.getResponseCode()) {
ins = urlConn.getInputStream();
ByteArrayOutputStream outs = new ByteArrayOutputStream(1024);
IOUtils.copy(ins, outs);
return outs.toString("UTF-8");
}
} catch (IOException e) {
throw e;
} finally {
IOUtils.closeQuietly(ins);
}
return null;
}
}

用Jetty实现的Http Server Mock

源码下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cn.aofeng.demo.jetty;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
/**
* HTTP服务器MOCK,可用于单元测试时模拟HTTP服务器的响应。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class HttpServerMock {
public final static int DEFAULT_PORT = 9191;
public final static String DEFAULT_CONTENT_TYPE = "application/json";
public final static int DEFAULT_STATUS_CODE=HttpServletResponse.SC_OK;
private Server _httpServer;
private int _port;
public HttpServerMock() {
_port = DEFAULT_PORT;
}
public HttpServerMock(int port) {
_port = port;
}
/**
* 启动Jetty服务器。默认的响应status code为"200",content type为"application/json"。
* @param content 响应内容
*/
public void start(String content) throws Exception {
start(content, DEFAULT_CONTENT_TYPE, DEFAULT_STATUS_CODE);
}
/**
* 启动Jetty服务器。默认的响应status code为"200"。
* @param content 响应内容
* @param contentType 响应内容的MIME类型
*/
public void start(String content, String contentType) throws Exception {
start(content, contentType, DEFAULT_STATUS_CODE);
}
/**
* 启动Jetty服务器。
* @param content 响应内容
* @param contentType 响应内容的MIME类型
* @param statuCode 响应状态码
*/
public void start(String content, String contentType,
int statuCode) throws Exception {
_httpServer = new Server(_port);
_httpServer.setHandler(createHandler(content, contentType, statuCode));
_httpServer.start();
}
/**
* 停止Jetty服务器。
*/
public void stop() throws Exception {
if (null != _httpServer) {
_httpServer.stop();
_httpServer = null;
}
}
private Handler createHandler(final String content, final String contentType,
final int statusCode) {
return new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
response.setContentType(contentType);
response.setStatus(statusCode);
baseRequest.setHandled(true);
response.getWriter().print(content);
}
};
}
}

单元测试代码

源码下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package cn.aofeng.demo.jetty;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
* {@link HttpGet}的单元测试用例。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class HttpGetTest {
private HttpServerMock _mock;
private HttpGet _httpGet = new HttpGet();
@Before
public void setUp() throws Exception {
_mock = new HttpServerMock();
}
@After
public void tearDown() throws Exception {
if (null != _mock) {
_mock.stop();
}
}
/**
* 用例:响应状态码为200且有响应内容。
*/
@Test
public void testGetSomeThing4Success() throws Exception {
String response = "Hello, The World!";
_mock.start(response, "text/plain");
String content = _httpGet.getSomeThing("http://localhost:9191/hello");
assertEquals(response, content);
}
/**
* 用例:响应状态码为非200。
*/
@Test
public void testGetSomeThing4Fail() throws Exception {
String response = "Hello, The World!";
_mock.start(response, "text/plain", 500);
String content = _httpGet.getSomeThing("http://localhost:9191/hello");
assertNull(content);
}
}