Skip to content

Commit 9b5508a

Browse files
committed
用EasyMock生成模拟对象进行单元测试
1 parent 9551ec5 commit 9b5508a

10 files changed

Lines changed: 143 additions & 0 deletions

‎lib/asm-3.1.jar‎

42 KB
Binary file not shown.

‎lib/asm-commons-3.1.jar‎

31.9 KB
Binary file not shown.

‎lib/asm-util-3.1.jar‎

33.6 KB
Binary file not shown.

‎lib/cglib-2.2.2.jar‎

280 KB
Binary file not shown.

‎lib/easymock-3.2.jar‎

117 KB
Binary file not shown.

‎lib/easymockclassextension-3.2.jar‎

10.2 KB
Binary file not shown.

‎lib/gson-2.2.4.jar‎

186 KB
Binary file not shown.

‎lib/objenesis-1.2.jar‎

35.2 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cn.aofeng.demo.easymock;
2+
3+
import java.io.IOException;
4+
import java.lang.reflect.Type;
5+
import java.util.Map;
6+
7+
import org.apache.log4j.Logger;
8+
9+
import com.google.gson.Gson;
10+
import com.google.gson.reflect.TypeToken;
11+
12+
import cn.aofeng.demo.jetty.HttpGet;
13+
14+
/**
15+
* 用户相关服务。如:获取用户昵称。
16+
*
17+
* @author <a href="mailto:[email protected]">聂勇</a>
18+
*/
19+
public class UserService {
20+
21+
private static Logger _logger = Logger.getLogger(UserService.class);
22+
23+
private HttpGet _httpGet = new HttpGet();
24+
25+
/**
26+
* 根据用户的账号ID获取昵称。
27+
*
28+
* @param accountId 用户的账号ID
29+
* @return 如果账号ID有效且请求成功,返回昵称;否则返回默认的昵称"用户xxx"。
30+
*/
31+
public String getNickname(String accountId) {
32+
String targetUrl = "http://192.168.56.102:8080/user?method=getNickname&accountId="+accountId;
33+
String response = null;
34+
try {
35+
response = _httpGet.getSomeThing(targetUrl);
36+
} catch (IOException e) {
37+
_logger.error("获取用户昵称时出错,账号ID:"+accountId, e);
38+
}
39+
40+
if (null != response) {
41+
// 响应数据结构示例:{"nickname":"张三"}
42+
Type type = new TypeToken<Map<String, String>>() {}.getType();
43+
Map<String, String> data = new Gson().fromJson(response, type);
44+
if (null != data && data.containsKey("nickname")) {
45+
return data.get("nickname");
46+
}
47+
}
48+
49+
return "用户"+accountId;
50+
}
51+
52+
protected void setHttpGet(HttpGet httpGet) {
53+
this._httpGet = httpGet;
54+
}
55+
56+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package cn.aofeng.demo.easymock;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.IOException;
6+
7+
import static org.easymock.EasyMock.*;
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import cn.aofeng.demo.jetty.HttpGet;
13+
14+
/**
15+
* {@link UserService}的单元测试用例。
16+
*
17+
* @author <a href="mailto:[email protected]">聂勇</a>
18+
*/
19+
public class UserServiceTest {
20+
21+
private HttpGet _mock = createMock(HttpGet.class);
22+
23+
@Before
24+
public void setUp() throws Exception {
25+
}
26+
27+
@After
28+
public void tearDown() throws Exception {
29+
reset(_mock);
30+
}
31+
32+
33+
/**
34+
* 测试用例:获取用户昵称 <br/>
35+
* 前置条件:
36+
* <pre>
37+
* 网络请求时发生IO异常
38+
* </pre>
39+
*
40+
* 测试结果:
41+
* <pre>
42+
* 返回默认的用户昵称"用户xxx"
43+
* </pre>
44+
*/
45+
@Test
46+
public void testGetNickname4OccursIOError() throws IOException {
47+
// 设置Mock
48+
expect(_mock.getSomeThing(anyString()))
49+
.andThrow(new IOException("单元测试特意抛的异常"));
50+
replay(_mock);
51+
52+
UserService us = new UserService();
53+
us.setHttpGet(_mock);
54+
String nickname = us.getNickname("123456");
55+
verify(_mock); // 校验mock
56+
assertEquals("用户123456", nickname); // 检查返回值
57+
}
58+
59+
/**
60+
* 测试用例:获取用户昵称 <br/>
61+
* 前置条件:
62+
* <pre>
63+
* 1、网络请求成功。
64+
* 2、响应状态码为200且响应内容符合接口定义({\"nickname\":\"张三\"})。
65+
* </pre>
66+
*
67+
* 测试结果:
68+
* <pre>
69+
* 返回"张三"
70+
* </pre>
71+
*/
72+
@Test
73+
public void testGetNickname4Success() throws IOException {
74+
// 设置Mock
75+
_mock.getSomeThing(anyString());
76+
expectLastCall().andReturn("{\"nickname\":\"张三\"}");
77+
expectLastCall().times(1);
78+
replay(_mock);
79+
80+
UserService us = new UserService();
81+
us.setHttpGet(_mock);
82+
String nickname = us.getNickname("123456");
83+
verify(_mock); // 校验方法的调用次数
84+
assertEquals("张三", nickname); // 校验返回值
85+
}
86+
87+
}

0 commit comments

Comments
 (0)