forked from aofeng/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpGetTest.java
More file actions
66 lines (52 loc) · 1.67 KB
/
HttpGetTest.java
File metadata and controls
66 lines (52 loc) · 1.67 KB
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
package cn.aofeng.demo.wiremock;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import cn.aofeng.demo.jetty.HttpGet;
/**
* {@link HttpGet}的单元测试用例。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class HttpGetTest {
private HttpGet _httpGet = new HttpGet();
@Rule
public WireMockRule _wireMockRule = new WireMockRule(9191);
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
/**
* 用例:响应状态码为200且有响应内容。
*/
@Test
public void testGetSomeThing4Success() throws Exception {
// 设置Mock
String response = "Hello, The World!";
stubFor(get(urlEqualTo("/hello"))
.willReturn(aResponse()
.withStatus(200)
.withBody(response)));
String content = _httpGet.getSomeThing("http://localhost:9191/hello");
assertEquals(response, content);
}
/**
* 用例:响应状态码为非200。
*/
@Test
public void testGetSomeThing4Fail() throws Exception {
// 设置Mock
stubFor(get(urlEqualTo("/hello"))
.willReturn(aResponse()
.withStatus(500)
.withBody("Hello, The World!")));
String content = _httpGet.getSomeThing("http://localhost:9191/hello");
assertNull(content);
}
}