mirror of
https://github.com/Estom/notes.git
synced 2026-02-07 12:35:20 +08:00
Java内容重新整理删除过期的东西
This commit is contained in:
50
Java/JavaDemo/codedemo/mockito/Commodity.java
Normal file
50
Java/JavaDemo/codedemo/mockito/Commodity.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package cn.aofeng.demo.mockito;
|
||||
|
||||
/**
|
||||
* 商品信息。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class Commodity {
|
||||
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private int type;
|
||||
|
||||
public Commodity() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
public Commodity(String id, String name, int type) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(int type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
}
|
||||
20
Java/JavaDemo/codedemo/mockito/CommodityDao.java
Normal file
20
Java/JavaDemo/codedemo/mockito/CommodityDao.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package cn.aofeng.demo.mockito;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品存储层。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class CommodityDao {
|
||||
|
||||
public Commodity queryById(String id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Commodity> queryByType(int type) {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
70
Java/JavaDemo/codedemo/mockito/User.java
Normal file
70
Java/JavaDemo/codedemo/mockito/User.java
Normal file
@@ -0,0 +1,70 @@
|
||||
package cn.aofeng.demo.mockito;
|
||||
|
||||
/**
|
||||
* 用户信息。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class User {
|
||||
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
private int sex;
|
||||
|
||||
private int age;
|
||||
|
||||
private String address;
|
||||
|
||||
public User() {
|
||||
// nothing
|
||||
}
|
||||
|
||||
public User(long userId, String userName, int age) {
|
||||
this.id = userId;
|
||||
this.name = userName;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getSex() {
|
||||
return sex;
|
||||
}
|
||||
|
||||
public void setSex(int sex) {
|
||||
this.sex = sex;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
}
|
||||
19
Java/JavaDemo/codedemo/mockito/UserDao.java
Normal file
19
Java/JavaDemo/codedemo/mockito/UserDao.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package cn.aofeng.demo.mockito;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户信息存储层。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class UserDao {
|
||||
|
||||
public User queryById(long userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<User> queryByName(String userName) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
54
Java/JavaDemo/codedemo/mockito/UserService.java
Normal file
54
Java/JavaDemo/codedemo/mockito/UserService.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package cn.aofeng.demo.mockito;
|
||||
|
||||
/**
|
||||
* 用户服务。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class UserService {
|
||||
|
||||
/** 成人年龄分界线 */
|
||||
private final static int ADULT_AGE = 18;
|
||||
|
||||
private UserDao _userDao;
|
||||
private CommodityDao _commodityDao;
|
||||
|
||||
public boolean isAdult(long userId) {
|
||||
User user = _userDao.queryById(userId);
|
||||
if (null == user || user.getAge() < ADULT_AGE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean buy(long userId, String commodityId) {
|
||||
if (! isAdult(userId)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Commodity commodity = _commodityDao.queryById(commodityId);
|
||||
if (null == commodity) {
|
||||
return false;
|
||||
}
|
||||
// 省略余下的处理逻辑
|
||||
return true;
|
||||
}
|
||||
|
||||
public UserDao getUserDao() {
|
||||
return _userDao;
|
||||
}
|
||||
|
||||
public void setUserDao(UserDao userDao) {
|
||||
this._userDao = userDao;
|
||||
}
|
||||
|
||||
public CommodityDao getCommodityDao() {
|
||||
return _commodityDao;
|
||||
}
|
||||
|
||||
public void setCommodityDao(CommodityDao commodityDao) {
|
||||
this._commodityDao = commodityDao;
|
||||
}
|
||||
|
||||
}
|
||||
66
Java/JavaDemo/codedemo/mockito/UserServiceTest.java
Normal file
66
Java/JavaDemo/codedemo/mockito/UserServiceTest.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package cn.aofeng.demo.mockito;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/**
|
||||
* {@link UserService}的单元测试用例。
|
||||
*
|
||||
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
|
||||
*/
|
||||
public class UserServiceTest {
|
||||
|
||||
private UserService _userService = new UserService();
|
||||
|
||||
@Test
|
||||
public void testIsAdult4UserExist() {
|
||||
long userId = 123;
|
||||
User user = new User(userId, "张三", 19);
|
||||
|
||||
// 大于18岁的测试用例
|
||||
UserDao daoMock = mock(UserDao.class);
|
||||
when(daoMock.queryById(userId)).thenReturn(user); // 设置行为和对应的返回值
|
||||
_userService.setUserDao(daoMock); // 设置mock
|
||||
assertTrue(_userService.isAdult(userId)); // 校验结果
|
||||
|
||||
// 等于18岁的测试用例
|
||||
User user2 = new User(userId, "李四", 18);
|
||||
when(daoMock.queryById(userId)).thenReturn(user2);
|
||||
_userService.setUserDao(daoMock);
|
||||
assertTrue(_userService.isAdult(userId));
|
||||
|
||||
// 小于18岁的测试用例
|
||||
User user3 = new User(userId, "王五", 17);
|
||||
when(daoMock.queryById(userId)).thenReturn(user3);
|
||||
_userService.setUserDao(daoMock);
|
||||
assertFalse(_userService.isAdult(userId));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsAdult4UserNotExist() {
|
||||
// 用户不存在的测试用例
|
||||
long userId = 123;
|
||||
UserDao daoMock = mock(UserDao.class);
|
||||
when(daoMock.queryById(userId)).thenReturn(null); // 设置行为和对应的返回值
|
||||
_userService.setUserDao(daoMock); // 设置mock
|
||||
assertFalse(_userService.isAdult(userId)); // 校验结果
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuy() {
|
||||
long userId = 12345;
|
||||
UserDao daoMock = mock(UserDao.class);
|
||||
when(daoMock.queryById(anyLong())).thenReturn(new User(userId, "张三", 19));
|
||||
|
||||
String commodityId = "S01A10009823";
|
||||
CommodityDao commodityDao = mock(CommodityDao.class);
|
||||
when(commodityDao.queryById(anyString())).thenReturn(new Commodity(commodityId, "xxx手机", 1));
|
||||
|
||||
_userService.setUserDao(daoMock);
|
||||
_userService.setCommodityDao(commodityDao);
|
||||
assertTrue(_userService.buy(userId, commodityId));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user