1.目录(有一套相同的test目录)
2.注解
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringBootTest(classes = UserApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
3.注意事项
第一:注意每个test都是独立的,before和after会在每个测试用例前后执行;
第二:当通过controller层测试时(this.testRestTemplate.postForEntity),测试类不能继承
AbstractTransactionalJUnit4SpringContextTests,否则造成请求失败,暂不清楚什么原因
/**
* <一句话功能简述> 测试添加买手组成功 信息正确
* author: zhanggw
* 创建时间: 2020/1/3
*/
@Test
public void test1AddUserSpecialGroup(){
try {
logger.debug("开始测试添加买手组!");
String reqUrl = this.base+"userspecial/addspecialusergroup"; // 添加买手组接口地址
logger.debug("接口地址:{}", reqUrl);
// 请求参数
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
String groupName = "测试买手组1";
JSONObject reqData = new JSONObject();
reqData.put("groupName", groupName); // 请求当前页码
params.add("data", reqData.toJSONString());
logger.debug("接口参数:{}", params.toSingleValueMap());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params,httpHeaders);
// 返回结果
ResponseEntity<JSONObject> retResponse = this.testRestTemplate.postForEntity(reqUrl, requestEntity, JSONObject.class);
JSONObject bodyJson = retResponse.getBody();
logger.debug("接口响应:{}", bodyJson.toJSONString());
boolean respFlag = bodyJson.getBoolean("flag");
Assert.assertSame("接口响应必须成功",respFlag,true);
// 从接口返回中获取当前添加用户组ID
buyerGroupId1 = bodyJson.getJSONObject("data").getString("specialGroupId");
// 根据ID从数据库获取信息
UserSpecialGroup userGroupDatabase = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
// 测试买手组信息是否正确
Assert.assertEquals("组名称必须相同",groupName, userGroupDatabase.getGroupName());
Assert.assertEquals("组类型必须为默认值1",TableStaticConstData.TABLE_USERSPECIAL_USERTYPE_BUYER, userGroupDatabase.getUserType().intValue());
Assert.assertEquals("组状态必须为默认值1",TableStaticConstData.TABLE_USERSPECIALGROUP_GROUPSTATE_VALID, userGroupDatabase.getGroupState().intValue());
Assert.assertNotNull("修改时间不能为空", userGroupDatabase.getModifydate());
Assert.assertNotNull("创建时间不能为空", userGroupDatabase.getCreateDate());
Assert.assertEquals("有效组员数默认为0",0, userGroupDatabase.getValidSum().intValue());
logger.debug("测试通过!");
} catch (Exception e) {
logger.debug(e.getMessage());
Assert.fail("出现异常,测试失败!");
}
}
5..整体代码
package com.ybjdw.user;
import com.alibaba.fastjson.JSONObject;
import com.ybjdw.base.constant.SysStaticConstData;
import com.ybjdw.base.constant.TableStaticConstData;
import com.ybjdw.user.userinfo.SV.interfaces.IUserSpecialSV;
import com.ybjdw.user.userinfo.bean.UserInfo;
import com.ybjdw.user.userinfo.bean.UserSpecial;
import com.ybjdw.user.userinfo.bean.UserSpecialGroup;
import com.ybjdw.user.userinfo.dao.UserInfoMapper;
import com.ybjdw.user.userinfo.dao.UserSpecialGroupMapper;
import com.ybjdw.user.userinfo.dao.UserSpecialMapper;
import com.ybjdw.user.utils.UniqueIdGenerator;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import java.net.URL;
@RunWith(SpringRunner.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@SpringBootTest(classes = UserApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class UserSpecialTests {
private Logger logger = LoggerFactory.getLogger(UserApplicationTests.class);
@Autowired
private TestRestTemplate testRestTemplate;
private URL base;
@LocalServerPort
private int port;
@Autowired(required = false)
private UserInfoMapper userInfoMapper;
@Autowired(required = false)
private UserSpecialMapper userSpecialMapper;
@Autowired(required = false)
private UserSpecialGroupMapper userSpecialGroupMapper;
@Autowired
private IUserSpecialSV userSpecialSV;
@Autowired
private UniqueIdGenerator uniqueIdGenerator;
private String userId1 = "";
private String userId2 = "";
private String buyerGroupId1 = "";
private String buyerGroupId2 = "";
@Before
public void setUp() throws Exception{
logger.debug("测试前环境准备");
String url = String.format("http://localhost:%d/", port); // 本地应用地址
this.base = new URL(url);
logger.debug("本地服务器url:{}", base.toString());
// 创建2个测试用户,只需要信息 user_id phone user_name,测试后删除
UserInfo userInfo = new UserInfo();
userId1 = uniqueIdGenerator.generateId(SysStaticConstData.ID_INDX3_USERINF);
userInfo.setUserId(userId1);
userInfo.setPhone("18502026601");
userInfo.setUserName("测试用户1");
userInfoMapper.insert(userInfo);
userId2 = uniqueIdGenerator.generateId(SysStaticConstData.ID_INDX3_USERINF);
userInfo.setUserId(userId2);
userInfo.setPhone("18502026602");
userInfo.setUserName("测试用户2");
userInfoMapper.insert(userInfo);
}
/**
* <一句话功能简述> 测试添加买手组成功 信息正确
* author: zhanggw
* 创建时间: 2020/1/3
*/
@Test
public void test1AddUserSpecialGroup(){
try {
logger.debug("开始测试添加买手组!");
String reqUrl = this.base+"userspecial/addspecialusergroup"; // 添加买手组接口地址
logger.debug("接口地址:{}", reqUrl);
// 请求参数
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
String groupName = "测试买手组1";
JSONObject reqData = new JSONObject();
reqData.put("groupName", groupName); // 请求当前页码
params.add("data", reqData.toJSONString());
logger.debug("接口参数:{}", params.toSingleValueMap());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params,httpHeaders);
// 返回结果
ResponseEntity<JSONObject> retResponse = this.testRestTemplate.postForEntity(reqUrl, requestEntity, JSONObject.class);
JSONObject bodyJson = retResponse.getBody();
logger.debug("接口响应:{}", bodyJson.toJSONString());
boolean respFlag = bodyJson.getBoolean("flag");
Assert.assertSame("接口响应必须成功",respFlag,true);
// 从接口返回中获取当前添加用户组ID
buyerGroupId1 = bodyJson.getJSONObject("data").getString("specialGroupId");
// 根据ID从数据库获取信息
UserSpecialGroup userGroupDatabase = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
// 测试买手组信息是否正确
Assert.assertEquals("组名称必须相同",groupName, userGroupDatabase.getGroupName());
Assert.assertEquals("组类型必须为默认值1",TableStaticConstData.TABLE_USERSPECIAL_USERTYPE_BUYER, userGroupDatabase.getUserType().intValue());
Assert.assertEquals("组状态必须为默认值1",TableStaticConstData.TABLE_USERSPECIALGROUP_GROUPSTATE_VALID, userGroupDatabase.getGroupState().intValue());
Assert.assertNotNull("修改时间不能为空", userGroupDatabase.getModifydate());
Assert.assertNotNull("创建时间不能为空", userGroupDatabase.getCreateDate());
Assert.assertEquals("有效组员数默认为0",0, userGroupDatabase.getValidSum().intValue());
logger.debug("测试通过!");
} catch (Exception e) {
logger.debug(e.getMessage());
Assert.fail("出现异常,测试失败!");
}
}
/**
* <一句话功能简述> 测试添加买手成功,买手和买手组信息正确
* author: zhanggw
* 创建时间: 2020/1/3
*/
@Test
public void test2AddUserSpecial(){
try {
// 添加买手组(通过service添加,方便)
JSONObject reqJson = new JSONObject();
reqJson.put("groupName", "测试买手组2");
UserSpecialGroup respGroup = userSpecialSV.addSpecialUserGroup(reqJson);
Assert.assertNotNull("添加买手组返回不能为null", respGroup);
buyerGroupId1 = respGroup.getSpecialGroupId();
logger.debug("开始测试添加买手并加入买手组!");
String reqUrl = this.base+"userspecial/addspecialuser"; // 添加买手组接口地址
logger.debug("接口地址:{}", reqUrl);
// 请求参数
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
JSONObject reqData = new JSONObject();
reqData.put("userSpecialId", userId1);
reqData.put("userLevel", 1);
reqData.put("specialGroupId", buyerGroupId1);
params.add("data", reqData.toJSONString());
logger.debug("接口参数:{}", params.toSingleValueMap());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params,httpHeaders);
// 返回结果
ResponseEntity<JSONObject> retResponse = this.testRestTemplate.postForEntity(reqUrl, requestEntity, JSONObject.class);
JSONObject bodyJson = retResponse.getBody();
logger.debug("接口响应:{}", bodyJson.toJSONString());
boolean respFlag = bodyJson.getBoolean("flag");
Assert.assertSame("接口响应必须成功",respFlag,true);
// 从数据库查询并判断
UserSpecial userSpecial = userSpecialMapper.selectByPrimaryKey(userId1);
Assert.assertEquals("用户id必须与添加时保持一致",userId1, userSpecial.getUserSpecialId());
Assert.assertEquals("用户等级必须与添加时保持一致",1,userSpecial.getUserLevel().intValue());
Assert.assertEquals("组信息必须与添加时保持一致",buyerGroupId1,userSpecial.getSpecialGroupId());
// 自动补足买手信息成功
UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId1);
Assert.assertEquals("买手名称为空时,自动使用原用户名称",userInfo.getUserName(),userSpecial.getUserName());
Assert.assertEquals("买手电话为空时,自动使用原用户电话",userInfo.getPhone(),userSpecial.getContactPhone());
// 买手组信息正确:组长id、姓名
UserSpecialGroup userSpecialGroup = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
Assert.assertEquals("买手组组长ID必须正确",userSpecial.getUserSpecialId(), userSpecialGroup.getGroupLeaderId());
Assert.assertEquals("买手组组长名称必须正确",userSpecial.getUserName(), userSpecialGroup.getGroupLeaderName());
Assert.assertEquals("加入第1个买手,买手组有效组员数为1",1, userSpecialGroup.getValidSum().intValue());
} catch (Exception e) {
logger.debug(e.getMessage());
Assert.fail("出现异常,测试失败!");
}
}
/**
* <一句话功能简述> 买手组长竞选场景1
* <功能详细描述> 先添加用户1为买手组长,再添加用户2为买手组长,最终组长应该为用户22
* author: zhanggw
* 创建时间: 2020/1/3
*/
@Test
public void test3GroupLeaderCompetition1(){
try {
// 添加买手组
JSONObject reqJson = new JSONObject();
reqJson.put("groupName", "测试买手组3");
UserSpecialGroup respGroup = userSpecialSV.addSpecialUserGroup(reqJson);
Assert.assertNotNull("添加买手组返回不能为null", respGroup);
buyerGroupId1 = respGroup.getSpecialGroupId();
logger.debug("添加用户1为买手组长");
JSONObject reqData = new JSONObject();
reqData.put("userSpecialId", userId1);
reqData.put("userLevel", 1);
reqData.put("specialGroupId", buyerGroupId1);
UserSpecial userResp = userSpecialSV.addSpecialUserInfo(reqData);
Assert.assertNotNull("添加买手返回不能为null", userResp);
// 买手信息正确
UserSpecial userSpecial1 = userSpecialMapper.selectByPrimaryKey(userId1);
Assert.assertEquals("买手1等级为1",1, userSpecial1.getUserLevel().intValue());
// 买手组信息正确:组长id、姓名
UserSpecialGroup userSpecialGroup = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
Assert.assertEquals("买手组组长ID必须正确",userId1, userSpecialGroup.getGroupLeaderId());
Assert.assertEquals("买手组组长名称必须正确",userSpecial1.getUserName(), userSpecialGroup.getGroupLeaderName());
Assert.assertEquals("买手组有效组员数为1",1, userSpecialGroup.getValidSum().intValue());
logger.debug("添加用户2为买手组长");
reqData = new JSONObject();
reqData.put("userSpecialId", userId2);
reqData.put("userLevel", 1);
reqData.put("specialGroupId", buyerGroupId1);
userResp = userSpecialSV.addSpecialUserInfo(reqData);
Assert.assertNotNull("添加买手返回不能为null", userResp);
UserSpecial userSpecial2 = userSpecialMapper.selectByPrimaryKey(userId2);
Assert.assertEquals("用户2等级为1",1, userSpecial2.getUserLevel().intValue());
// 买手组信息正确:组长id、姓名
userSpecialGroup = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
Assert.assertEquals("买手组组长ID必须正确",userId2, userSpecialGroup.getGroupLeaderId());
Assert.assertEquals("买手组组长名称必须正确",userSpecial2.getUserName(), userSpecialGroup.getGroupLeaderName());
Assert.assertEquals("买手组有效组员数为2",2, userSpecialGroup.getValidSum().intValue());
// 用户1降级为3
userSpecial1 = userSpecialMapper.selectByPrimaryKey(userId1);
Assert.assertEquals("用户1等级为3",3, userSpecial1.getUserLevel().intValue());
} catch (Exception e) {
logger.debug(e.getMessage());
Assert.fail("出现异常,测试失败!");
}
}
/**
* <一句话功能简述> 买手组长竞选场景2
* <功能详细描述> 先添加用户1为买手组长,再添加用户2为副组长,修改用户2等级为组长,最终组长为用户2
* author: zhanggw
* 创建时间: 2020/1/3
*/
@Test
public void test4GroupLeaderCompetition2(){
try {
// 添加买手组
JSONObject reqJson = new JSONObject();
reqJson.put("groupName", "测试买手组4");
UserSpecialGroup respGroup = userSpecialSV.addSpecialUserGroup(reqJson);
Assert.assertNotNull("添加买手组返回不能为null", respGroup);
buyerGroupId1 = respGroup.getSpecialGroupId();
logger.debug("添加用户1为买手组长");
JSONObject reqData = new JSONObject();
reqData.put("userSpecialId", userId1);
reqData.put("userLevel", 1);
reqData.put("specialGroupId", buyerGroupId1);
UserSpecial user1Resp = userSpecialSV.addSpecialUserInfo(reqData);
Assert.assertNotNull("添加买手返回不能为null", user1Resp);
logger.debug("添加用户2为副组长");
reqData = new JSONObject();
reqData.put("userSpecialId", userId2);
reqData.put("userLevel", 2);
reqData.put("specialGroupId", buyerGroupId1);
UserSpecial user2Resp = userSpecialSV.addSpecialUserInfo(reqData);
Assert.assertNotNull("添加买手返回不能为null", user2Resp);
UserSpecial userSpecial2 = userSpecialMapper.selectByPrimaryKey(userId2);
Assert.assertEquals("买手2等级为2",2, userSpecial2.getUserLevel().intValue());
// 买手组信息正确:组长id、姓名
UserSpecialGroup userSpecialGroup = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
Assert.assertEquals("买手组组长ID必须正确",userId1, userSpecialGroup.getGroupLeaderId());
Assert.assertEquals("买手组组长名称必须正确",user1Resp.getUserName(), userSpecialGroup.getGroupLeaderName());
// 修改副组长等级
String reqUrl = this.base+"userspecial/updatespecialuserbyid"; // 修改买手接口地址
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
reqData = new JSONObject();
reqData.put("userSpecialId", userId2); // 用户ID
reqData.put("userLevel", 1); // 用户等级1 组长
params.add("data", reqData.toJSONString());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params,httpHeaders);
ResponseEntity<JSONObject> retResponse = this.testRestTemplate.postForEntity(reqUrl, requestEntity, JSONObject.class);
JSONObject bodyJson = retResponse.getBody();
logger.debug("response:"+bodyJson.toJSONString());
// 接口成功响应
boolean respFlag = bodyJson.getBoolean("flag");
Assert.assertTrue("修改买手接口调用必须成功!",respFlag);
Assert.assertEquals("操作结果必须为操作成功","操作成功!",bodyJson.getString("message"));
userSpecial2 = userSpecialMapper.selectByPrimaryKey(userId2);
Assert.assertEquals("用户2买手等级提升为1",1, userSpecial2.getUserLevel().intValue());
userSpecialGroup = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
Assert.assertEquals("买手组组长ID为用户2 ID",userId2, userSpecialGroup.getGroupLeaderId());
Assert.assertEquals("买手组组长名称为用户2名称)",userSpecial2.getUserName(), userSpecialGroup.getGroupLeaderName());
Assert.assertEquals("买手组有效组员数为2",2, userSpecialGroup.getValidSum().intValue());
// 用户1降级为3
UserSpecial userSpecial1 = userSpecialMapper.selectByPrimaryKey(userId1);
Assert.assertEquals("用户1等级为3",3, userSpecial1.getUserLevel().intValue());
} catch (Exception e) {
logger.debug(e.getMessage());
Assert.fail("出现异常,测试失败!");
}
}
/**
* <一句话功能简述> 买手组长竞选场景3
* <功能详细描述> 先添加用户1为买手组长,再添加用户2为普通买手,修改买手组组长为用户2
* author: zhanggw
* 创建时间: 2020/1/3
*/
@Test
public void test5GroupLeaderCompetition3(){
try {
// 添加买手组
JSONObject reqJson = new JSONObject();
reqJson.put("groupName", "测试买手组4");
UserSpecialGroup respGroup = userSpecialSV.addSpecialUserGroup(reqJson);
Assert.assertNotNull("添加买手组返回不能为null", respGroup);
buyerGroupId1 = respGroup.getSpecialGroupId();
logger.debug("添加用户1为买手组长");
JSONObject reqData = new JSONObject();
reqData.put("userSpecialId", userId1);
reqData.put("userLevel", 1);
reqData.put("specialGroupId", buyerGroupId1);
UserSpecial user1Resp = userSpecialSV.addSpecialUserInfo(reqData);
Assert.assertNotNull("添加买手返回不能为null", user1Resp);
logger.debug("添加用户2为普通买手");
reqData = new JSONObject();
reqData.put("userSpecialId", userId2);
reqData.put("userLevel", 3);
reqData.put("specialGroupId", buyerGroupId1);
UserSpecial user2Resp = userSpecialSV.addSpecialUserInfo(reqData);
Assert.assertNotNull("添加买手返回不能为null", user2Resp);
UserSpecial userSpecial2 = userSpecialMapper.selectByPrimaryKey(userId2);
Assert.assertEquals("买手2等级为3",3, userSpecial2.getUserLevel().intValue());
// 买手组信息正确:组长id、姓名
UserSpecialGroup userSpecialGroup = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
Assert.assertEquals("买手组组长ID必须正确",userId1, userSpecialGroup.getGroupLeaderId());
Assert.assertEquals("买手组组长名称必须正确",user1Resp.getUserName(), userSpecialGroup.getGroupLeaderName());
// 修改买手组,指定用户2为组长
String reqUrl = this.base+"userspecial/updatespecialusergroupbyid"; // 修改买手接口地址
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
reqData = new JSONObject();
reqData.put("specialGroupId", buyerGroupId1); // 用户1 ID
reqData.put("groupLeaderId", userId2); // 用户等级1 组长
params.add("data", reqData.toJSONString());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params,httpHeaders);
ResponseEntity<JSONObject> retResponse = this.testRestTemplate.postForEntity(reqUrl, requestEntity, JSONObject.class);
JSONObject bodyJson = retResponse.getBody();
logger.debug("response:"+bodyJson.toJSONString());
// 接口成功响应
boolean respFlag = bodyJson.getBoolean("flag");
Assert.assertTrue("修改买手接口调用成功!",respFlag);
Assert.assertEquals("操作结果必须为操作成功","操作成功!",bodyJson.getString("message"));
userSpecial2 = userSpecialMapper.selectByPrimaryKey(userId2);
Assert.assertEquals("用户2买手等级提升为1",1, userSpecial2.getUserLevel().intValue());
userSpecialGroup = userSpecialGroupMapper.selectByPrimaryKey(buyerGroupId1);
Assert.assertEquals("买手组组长ID为用户2 ID",userId2, userSpecialGroup.getGroupLeaderId());
Assert.assertEquals("买手组组长名称为用户2名称)",userSpecial2.getUserName(), userSpecialGroup.getGroupLeaderName());
Assert.assertEquals("买手组有效组员数为2",2, userSpecialGroup.getValidSum().intValue());
// 用户1降级为3
UserSpecial userSpecial1 = userSpecialMapper.selectByPrimaryKey(userId1);
Assert.assertEquals("用户1等级为3",3, userSpecial1.getUserLevel().intValue());
} catch (Exception e) {
logger.debug(e.getMessage());
Assert.fail("出现异常,测试失败!");
}
}
/**
* <一句话功能简述> 测试特殊用户组查询
* author: zhanggw
* 创建时间: 2020/1/3
*/
@Test
public void test6UserSpecialGroupQueryList(){
try {
// 先添加买手组
JSONObject reqJson = new JSONObject();
reqJson.put("groupName", "测试买手组20");
UserSpecialGroup respEntity = userSpecialSV.addSpecialUserGroup(reqJson);
Assert.assertNotNull("买手组为null,添加买手组失败", respEntity);
buyerGroupId1 = respEntity.getSpecialGroupId();
reqJson.put("groupName", "测试买手组21");
respEntity = userSpecialSV.addSpecialUserGroup(reqJson);
Assert.assertNotNull("买手组为null,添加买手组失败", respEntity);
buyerGroupId2 = respEntity.getSpecialGroupId();
String reqUrl = this.base+"userspecial/queryspecialusergroup"; // 查询买手组接口地址
// 请求参数
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
JSONObject reqData = new JSONObject();
int reqPageNum = 1;
int reqPageSize = 1;
reqData.put("pageNumber", reqPageNum); // 请求当前页码
reqData.put("pageSize", reqPageSize); // 请求当前页大小
params.add("data", reqData.toJSONString());
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params,httpHeaders);
// 返回结果
ResponseEntity<JSONObject> retResponse = this.testRestTemplate.postForEntity(reqUrl, requestEntity, JSONObject.class);
JSONObject bodyJson = retResponse.getBody();
logger.debug("response:"+bodyJson.toJSONString());
// 断言 接口成功响应
boolean respFlag = bodyJson.getBoolean("flag");
Assert.assertSame(respFlag,true);
// 断言 响应页码和页大小
int respPageNum = bodyJson.getIntValue("pageNumber");
int respPageSize = bodyJson.getIntValue("pageSize");
Assert.assertSame(respPageNum,reqPageNum);
Assert.assertSame(respPageSize,reqPageSize);
// 断言请求结果数
int respDataSize = bodyJson.getJSONArray("data").size();
long respTotal = bodyJson.getLongValue("total");
long expectDataSize = respTotal >= reqPageSize ? reqPageSize : respTotal;
Assert.assertSame(respDataSize, Long.valueOf(expectDataSize).intValue());
}catch (Exception e){
logger.debug(e.getMessage());
Assert.fail("测试异常!");
}
}
@After
public void tearDown(){
logger.debug("开始清理环境数据!");
if(!StringUtils.isEmpty(userId1)){
userInfoMapper.deleteByPrimaryKey(userId1);
userSpecialMapper.deleteByPrimaryKey(userId1);
}
if(!StringUtils.isEmpty(userId2)){
userInfoMapper.deleteByPrimaryKey(userId2);
userSpecialMapper.deleteByPrimaryKey(userId2);
}
if(!StringUtils.isEmpty(buyerGroupId1)){
userSpecialGroupMapper.deleteByPrimaryKey(buyerGroupId1);
}
if(!StringUtils.isEmpty(buyerGroupId2)){
userSpecialGroupMapper.deleteByPrimaryKey(buyerGroupId2);
}
logger.debug("清理结束,测试结束!");
}
}
5.测试执行情况