Skip to content

Commit e4f9bd5

Browse files
committed
使用Mockito生成stub做单元测试
1 parent 0f4945b commit e4f9bd5

6 files changed

Lines changed: 279 additions & 0 deletions

File tree

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cn.aofeng.demo.mockito;
2+
3+
/**
4+
* 商品信息。
5+
*
6+
* @author <a href="mailto:[email protected]">聂勇</a>
7+
*/
8+
public class Commodity {
9+
10+
private String id;
11+
12+
private String name;
13+
14+
private int type;
15+
16+
public Commodity() {
17+
// nothing
18+
}
19+
20+
public Commodity(String id, String name, int type) {
21+
this.id = id;
22+
this.name = name;
23+
this.type = type;
24+
}
25+
26+
public String getId() {
27+
return id;
28+
}
29+
30+
public void setId(String id) {
31+
this.id = id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
42+
public int getType() {
43+
return type;
44+
}
45+
46+
public void setType(int type) {
47+
this.type = type;
48+
}
49+
50+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package cn.aofeng.demo.mockito;
2+
3+
import java.util.List;
4+
5+
/**
6+
* 商品存储层。
7+
*
8+
* @author <a href="mailto:[email protected]">聂勇</a>
9+
*/
10+
public class CommodityDao {
11+
12+
public Commodity queryById(String id) {
13+
return null;
14+
}
15+
16+
public List<Commodity> queryByType(int type) {
17+
return null;
18+
}
19+
20+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.aofeng.demo.mockito;
2+
3+
/**
4+
* 用户信息。
5+
*
6+
* @author <a href="mailto:[email protected]">聂勇</a>
7+
*/
8+
public class User {
9+
10+
private long id;
11+
12+
private String name;
13+
14+
private int sex;
15+
16+
private int age;
17+
18+
private String address;
19+
20+
public User() {
21+
// nothing
22+
}
23+
24+
public User(long userId, String userName, int age) {
25+
this.id = userId;
26+
this.name = userName;
27+
this.age = age;
28+
}
29+
30+
public long getId() {
31+
return id;
32+
}
33+
34+
public void setId(long id) {
35+
this.id = id;
36+
}
37+
38+
public String getName() {
39+
return name;
40+
}
41+
42+
public void setName(String name) {
43+
this.name = name;
44+
}
45+
46+
public int getSex() {
47+
return sex;
48+
}
49+
50+
public void setSex(int sex) {
51+
this.sex = sex;
52+
}
53+
54+
public int getAge() {
55+
return age;
56+
}
57+
58+
public void setAge(int age) {
59+
this.age = age;
60+
}
61+
62+
public String getAddress() {
63+
return address;
64+
}
65+
66+
public void setAddress(String address) {
67+
this.address = address;
68+
}
69+
70+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package cn.aofeng.demo.mockito;
2+
3+
import java.util.List;
4+
5+
/**
6+
* 用户信息存储层。
7+
*
8+
* @author <a href="mailto:[email protected]">聂勇</a>
9+
*/
10+
public class UserDao {
11+
12+
public User queryById(long userId) {
13+
return null;
14+
}
15+
16+
public List<User> queryByName(String userName) {
17+
return null;
18+
}
19+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package cn.aofeng.demo.mockito;
2+
3+
/**
4+
* 用户服务。
5+
*
6+
* @author <a href="mailto:[email protected]">聂勇</a>
7+
*/
8+
public class UserService {
9+
10+
/** 成人年龄分界线 */
11+
private final static int ADULT_AGE = 18;
12+
13+
private UserDao _userDao;
14+
private CommodityDao _commodityDao;
15+
16+
public boolean isAdult(long userId) {
17+
User user = _userDao.queryById(userId);
18+
if (null == user || user.getAge() < ADULT_AGE) {
19+
return false;
20+
}
21+
22+
return true;
23+
}
24+
25+
public boolean buy(long userId, String commodityId) {
26+
if (! isAdult(userId)) {
27+
return false;
28+
}
29+
30+
Commodity commodity = _commodityDao.queryById(commodityId);
31+
if (null == commodity) {
32+
return false;
33+
}
34+
// 省略余下的处理逻辑
35+
return true;
36+
}
37+
38+
public UserDao getUserDao() {
39+
return _userDao;
40+
}
41+
42+
public void setUserDao(UserDao userDao) {
43+
this._userDao = userDao;
44+
}
45+
46+
public CommodityDao getCommodityDao() {
47+
return _commodityDao;
48+
}
49+
50+
public void setCommodityDao(CommodityDao commodityDao) {
51+
this._commodityDao = commodityDao;
52+
}
53+
54+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package cn.aofeng.demo.mockito;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
import static org.mockito.Mockito.*;
7+
8+
/**
9+
* {@link UserService}的单元测试用例。
10+
*
11+
* @author <a href="mailto:[email protected]">聂勇</a>
12+
*/
13+
public class UserServiceTest {
14+
15+
private UserService _userService = new UserService();
16+
17+
@Test
18+
public void testIsAdult4UserExist() {
19+
long userId = 123;
20+
User user = new User(userId, "张三", 19);
21+
22+
// 大于18岁的测试用例
23+
UserDao daoMock = mock(UserDao.class);
24+
when(daoMock.queryById(userId)).thenReturn(user); // 设置行为和对应的返回值
25+
_userService.setUserDao(daoMock); // 设置mock
26+
assertTrue(_userService.isAdult(userId)); // 校验结果
27+
28+
// 等于18岁的测试用例
29+
User user2 = new User(userId, "李四", 18);
30+
when(daoMock.queryById(userId)).thenReturn(user2);
31+
_userService.setUserDao(daoMock);
32+
assertTrue(_userService.isAdult(userId));
33+
34+
// 小于18岁的测试用例
35+
User user3 = new User(userId, "王五", 17);
36+
when(daoMock.queryById(userId)).thenReturn(user3);
37+
_userService.setUserDao(daoMock);
38+
assertFalse(_userService.isAdult(userId));
39+
}
40+
41+
@Test
42+
public void testIsAdult4UserNotExist() {
43+
// 用户不存在的测试用例
44+
long userId = 123;
45+
UserDao daoMock = mock(UserDao.class);
46+
when(daoMock.queryById(userId)).thenReturn(null); // 设置行为和对应的返回值
47+
_userService.setUserDao(daoMock); // 设置mock
48+
assertFalse(_userService.isAdult(userId)); // 校验结果
49+
}
50+
51+
@Test
52+
public void testBuy() {
53+
long userId = 12345;
54+
UserDao daoMock = mock(UserDao.class);
55+
when(daoMock.queryById(userId)).thenReturn(new User(userId, "张三", 19));
56+
57+
String commodityId = "S01A10009823";
58+
CommodityDao commodityDao = mock(CommodityDao.class);
59+
when(commodityDao.queryById(commodityId)).thenReturn(new Commodity(commodityId, "xxx手机", 1));
60+
61+
_userService.setUserDao(daoMock);
62+
_userService.setCommodityDao(commodityDao);
63+
assertTrue(_userService.buy(userId, commodityId));
64+
}
65+
66+
}

0 commit comments

Comments
 (0)