1.測試對象:SpringBoot Dao層中各方法測試诚啃。
2.測試類開始前添加的注解:
@Transactional:每個@Test測試結(jié)束后,都對數(shù)據(jù)庫進行回滾驯遇,不會在數(shù)據(jù)庫中留下測試數(shù)據(jù)谱仪,也有助于@Test的獨立性。
@RunWith(SpringRunner.class)串远、@SpringBootTest是SpringBoot單元測試默認添加的注解宏多。后續(xù)再查下這兩個注解。
3.測試類中的單元測試用例舉例:
測試用例名稱:businessAddTest抑淫,被測試方法有addBusinessLine绷落、getBusinessLine。程序如下始苇。
編寫思路:
1)首先定義程序中要使用的參數(shù)。
2)調(diào)用需要測試的方法addBusinessLine(BusinessLine)筐喳。
3)對該方法的結(jié)果進行判斷:
Assert.assertEquals(SUCCESS, result);
注:Assert.assertEquals();及其重載方法: a. 如果兩者一致, 程序繼續(xù)往下運行. b. 如果兩者不一致, 中斷測試方法, 拋出異常信息 AssertionFailedError 催式。
4)調(diào)用需要測試的方法getBusinessLine(id)函喉,對數(shù)據(jù)庫的內(nèi)容進行判斷,判斷是否插入了這條數(shù)據(jù)荣月,也間接測試了方法getBusinessLine(id)管呵。其實這里也可以直接對數(shù)據(jù)庫進行查詢,查看是否插入了這條數(shù)據(jù)哺窄。businessGet中對數(shù)據(jù)庫獲取到的name捐下、creator、groupId萌业、status分別進行判斷坷襟,看是否是創(chuàng)建時傳入的參數(shù)。
@Test
public void businessAddTest() {
String name = "hongtest001";
String creator = "lihong";
int groupId = 1;
int status = 1;
//增加業(yè)務(wù)線
int id = Basic.businessAdd(name,creator,groupId,status);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupId,1);
//增加業(yè)務(wù)線 名稱重復(fù)
BusinessLine = Basic.getBusinessLine(name,creator,groupId,status);
SqlEnum result = BusinessLineDao.addBusinessLine(BusinessLine);
Assert.assertEquals(DUPLICATE, result);
System.out.println("重復(fù)增加業(yè)務(wù)線結(jié)果:"+result);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupId,1);
}
public void businessGet(int id,String name,String creator,int groupId,int status){
Map resultmap = BusinessLineDao.getBusinessLine(id);
//獲取某id業(yè)務(wù)線的名稱生年、創(chuàng)建者婴程,所屬團隊、狀態(tài)
SqlEnum result = (SqlEnum)resultmap.get("result");
BusinessLine businessLineGet = (BusinessLine) resultmap.get("businessLine");
String nameGet = businessLineGet.getBusinessLineName();
String creatorGet = businessLineGet.getBusinessLineCreator();
int groupIdGet = businessLineGet.getLinkGroupID();
int statusGet = businessLineGet.getBusinessLineStatus();
//判斷某id業(yè)務(wù)線的名稱抱婉、創(chuàng)建者档叔、所屬團隊、狀態(tài)是否正確
Assert.assertEquals(name, nameGet);
Assert.assertEquals(creator, creatorGet);
Assert.assertEquals(groupId, groupIdGet);
Assert.assertEquals(status, statusGet);
Assert.assertEquals(SUCCESS, result);
System.out.println("獲取業(yè)務(wù)線信息結(jié)果:"+result);
}
public int businessAdd(String name,String creator,int groupId,int status){
BusinessLine = getBusinessLine(name,creator,groupId,status);
SqlEnum result = BusinessLineDao.addBusinessLine(BusinessLine);
Assert.assertEquals(SUCCESS, result);
System.out.println("增加業(yè)務(wù)線結(jié)果:"+result);
Map businessMap = jdbcTemplate.queryForMap("select * from interface_business_lines where business_line_name=? and status = 1", name);
int id = (int)businessMap.get("id");
System.out.println("業(yè)務(wù)線id:"+ id);
return id;
}
4.針對BusinessLineDao類中的每一個方法蒸绩,分別寫出每個方法的測試用例衙四,即每一個方法的測試都以注解@Test開始。
5.對于重復(fù)使用的程序患亿,可以封裝成類中的方法届搁,方便調(diào)用。這里使用到的方法都放在Basic類中窍育。在Basic類開始前添加注解@Component卡睦,在使用的時候使用@Autowired Basic Basic來實例化。
6.添加打印漱抓,方便調(diào)試使用:System.out.println("獲取業(yè)務(wù)線信息結(jié)果:"+result);
7.附代碼:
BusinessLineDao.java
package com.xingshulin.dao;
import com.xingshulin.utils.DateUtil;
import com.xingshulin.utils.SqlEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate;
import com.xingshulin.entities.BusinessLine;
import org.springframework.stereotype.Repository;
import java.util.*;
@Repository
public class BusinessLineDao {
// 注入JdbcTemplate
@Autowired
private JdbcTemplate jdbcTemplate;
//增加業(yè)務(wù)線
public SqlEnum addBusinessLine(BusinessLine businessLine) {
SqlEnum result = SqlEnum.SUCCESS;
try {
jdbcTemplate.update("insert into interface_business_lines(business_line_name,creator,status," +
"create_time,group_id) values (?,?,?,?,?)", businessLine.getBusinessLineName(),
businessLine.getBusinessLineCreator(), businessLine.getBusinessLineStatus(),
DateUtil.timeStamp2Date(DateUtil.timeStamp(), "yyyy-MM-dd HH:mm:ss"), businessLine.getLinkGroupID());
} catch (DuplicateKeyException e) {
result = SqlEnum.DUPLICATE;
} catch (Exception e) {
result = SqlEnum.UNKNOWN;
}
return result;
}
//根據(jù)業(yè)務(wù)線id獲取業(yè)務(wù)線
public Map getBusinessLine(int id) {
SqlEnum result = SqlEnum.SUCCESS;
BusinessLine businessLine = new BusinessLine();
try {
Map businessMap = jdbcTemplate.queryForMap("select * from interface_business_lines where id=?", id);
businessLine.setBusinessLineID((int) businessMap.get("id"));
businessLine.setBusinessLineName((String) businessMap.get("business_line_name"));
businessLine.setBusinessLineCreator((String) businessMap.get("creator"));
businessLine.setBusinessLineCreateTime(businessMap.get("create_time").toString());
businessLine.setBusinessLineUpdateTime(businessMap.get("update_time").toString());
businessLine.setBusinessLineStatus((int) businessMap.get("status"));
businessLine.setLinkGroupID((int) businessMap.get("group_id"));
} catch (Exception e) {
result = SqlEnum.UNKNOWN;
}
Map map = new HashMap();
map.put("businessLine", businessLine);
map.put("result", result);
return map;
}
//根據(jù)業(yè)務(wù)線id刪除業(yè)務(wù)線
public SqlEnum deleteBusinessLine(int id) {
SqlEnum result = SqlEnum.SUCCESS;
try {
jdbcTemplate.update("update interface_business_lines set status=? where id=?" ,(-1)*id,id);
} catch (DuplicateKeyException e) {
result = SqlEnum.DUPLICATE;
} catch (Exception e) {
result = SqlEnum.UNKNOWN;
}
return result;
}
//移動業(yè)務(wù)線
public SqlEnum moveBusinessLine(int id, int groupIdAfter) {
SqlEnum result = SqlEnum.SUCCESS;
try {
jdbcTemplate.update("update interface_business_lines set group_id=? where id=?", groupIdAfter, id);
} catch (DuplicateKeyException e) {
result = SqlEnum.DUPLICATE;
} catch (Exception e) {
result = SqlEnum.UNKNOWN;
}
return result;
}
//修改業(yè)務(wù)線名稱
public SqlEnum updateBusinessLineName(int id, String business_line_name) {
SqlEnum result = SqlEnum.SUCCESS;
try {
jdbcTemplate.update("update interface_business_lines set business_line_name=? where id=?", business_line_name, id);
} catch (DuplicateKeyException e) {
result = SqlEnum.DUPLICATE;
} catch (Exception e) {
result = SqlEnum.UNKNOWN;
}
return result;
}
//獲取所有業(yè)務(wù)線信息-分頁獲取
public Map getBusinessLines(int group_id, int pageNum, int pageSize) {
SqlEnum result = SqlEnum.SUCCESS;
Map map =new HashMap();
try {
//統(tǒng)計status為1即未刪除的的業(yè)務(wù)線個數(shù)
int count = jdbcTemplate.queryForObject("select count(1) from interface_business_lines where group_id=? and status=1", Integer.class, group_id);
//從數(shù)據(jù)庫中獲取從第pageNum開始的表锻、pageSize大小的數(shù)據(jù)放到list中
List businesslineList = jdbcTemplate.queryForList("select * from interface_business_lines where status=1 and group_id=? order by create_time desc limit ?,?", group_id, (pageNum - 1) * pageSize, pageSize);
//獲取businesslineList的迭代器
Iterator<Map> iterator = businesslineList.iterator();
List<BusinessLine> businessLinesList = new ArrayList<>();
// Map businessLineMap = new HashMap();
while (iterator.hasNext()) {
Map businesslinetemp = iterator.next();
BusinessLine businessLine = new BusinessLine();
businessLine.setBusinessLineID((int) businesslinetemp.get("id"));
businessLine.setBusinessLineName(businesslinetemp.get("business_line_name").toString());
businessLine.setBusinessLineCreator(businesslinetemp.get("creator").toString());
businessLine.setBusinessLineCreateTime(businesslinetemp.get("create_time").toString());
businessLine.setBusinessLineUpdateTime(businesslinetemp.get("update_time").toString());
businessLine.setBusinessLineStatus((int) businesslinetemp.get("status"));
businessLine.setLinkGroupID((int) businesslinetemp.get("group_id"));
businessLinesList.add(businessLine);
}
map.put("businessLinesList", businessLinesList);
map.put("count", count);
map.put("result",result);
}catch(Exception e){
result=SqlEnum.UNKNOWN;
map.put("result",result);
}
return map;
}
// 獲取所有業(yè)務(wù)線的id和name
public Map getAllBusiIdandName(int group_id){
SqlEnum result = SqlEnum.SUCCESS;
Map map = new HashMap();
try{
int count = jdbcTemplate.queryForObject("select count(1) from interface_business_lines where group_id=? and status=1", Integer.class, group_id);
List list = jdbcTemplate.queryForList("select * from interface_business_lines where status=1 and group_id = ? order by create_time desc",group_id);
map.put("total",count);
map.put("list",list);
map.put("result",result);
}catch (Exception e){
result=SqlEnum.UNKNOWN;
map.put("result",result);
}
return map;
}
}
BusinessDaoTests.java
package com.xingshulin.BusinessDaoTests;
import com.xingshulin.entities.BusinessLine;
import com.xingshulin.utils.SqlEnum;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import com.xingshulin.dao.BusinessLineDao;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import static com.xingshulin.utils.SqlEnum.DUPLICATE;
import static com.xingshulin.utils.SqlEnum.SUCCESS;
//數(shù)據(jù)庫回滾
@Transactional
@RunWith(SpringRunner.class)
@SpringBootTest
public class BusinessDaoTests {
@Autowired
BusinessLineDao BusinessLineDao;
@Autowired
private JdbcTemplate jdbcTemplate;
@Autowired
Basic Basic;
BusinessLine BusinessLine = new BusinessLine();
@Test
public void businessAddTest() {
String name = "hongtest001";
String creator = "lihong";
int groupId = 1;
int status = 1;
//增加業(yè)務(wù)線
int id = Basic.businessAdd(name,creator,groupId,status);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupId,1);
//增加業(yè)務(wù)線 名稱重復(fù)
BusinessLine = Basic.getBusinessLine(name,creator,groupId,status);
SqlEnum result = BusinessLineDao.addBusinessLine(BusinessLine);
Assert.assertEquals(DUPLICATE, result);
System.out.println("重復(fù)增加業(yè)務(wù)線結(jié)果:"+result);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupId,1);
}
@Test
public void businessMoveTest() {
String name = "hongtest001";
String creator = "lihong";
int groupId = 1;
int groupIdAfter = 999;
int status = 1;
//增加業(yè)務(wù)線
int id = Basic.businessAdd(name,creator,groupId,status);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupId,1);
//移動業(yè)務(wù)線
SqlEnum result = BusinessLineDao.moveBusinessLine(id,groupIdAfter);
Assert.assertEquals(SUCCESS, result);
System.out.println("移動業(yè)務(wù)線結(jié)果:"+result);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupIdAfter,1);
}
@Test
public void businessUpdateTest() {
String name = "hongtest001";
String name2 = "hongtest002";
String nameAfter = "hongtest003";
String creator = "lihong";
int groupId = 1;
int status = 1;
//增加業(yè)務(wù)線1
int id = Basic.businessAdd(name,creator,groupId,status);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupId,1);
//增加業(yè)務(wù)線2
int id2 = Basic.businessAdd(name2,creator,groupId,status);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id2,name2,creator,groupId,1);
//更新業(yè)務(wù)線名稱為nameAfter
SqlEnum result = BusinessLineDao.updateBusinessLineName(id,nameAfter);
Assert.assertEquals(SUCCESS, result);
System.out.println("更新業(yè)務(wù)線結(jié)果:"+result);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,nameAfter,creator,groupId,1);
//更新業(yè)務(wù)線名稱,名稱與業(yè)務(wù)線2名稱重復(fù)
result = BusinessLineDao.updateBusinessLineName(id,name2);
Assert.assertEquals(DUPLICATE, result);
System.out.println("更新業(yè)務(wù)線結(jié)果:"+result);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,nameAfter,creator,groupId,1);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id2,name2,creator,groupId,1);
}
@Test
public void businessDeleteTest() {
String name = "hongtest001";
String creator = "lihong";
int groupId = 1;
int status = 1;
//增加業(yè)務(wù)線1
int id = Basic.businessAdd(name,creator,groupId,status);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupId,1);
//刪除業(yè)務(wù)線
SqlEnum result = BusinessLineDao.deleteBusinessLine(id);
Assert.assertEquals(SUCCESS, result);
System.out.println("刪除業(yè)務(wù)線結(jié)果:"+result);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name,creator,groupId,(-1)*id);
}
@Test
public void businessGetAllTest() {
String name[] = {"hongtest001","hongtest002"};
String creator[] = {"lihong","honghai"};
int groupId = 1000;
int status = 1;
//增加業(yè)務(wù)線1
int id = Basic.businessAdd(name[0],creator[0],groupId,status);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id,name[0],creator[0],groupId,1);
//增加業(yè)務(wù)線2
int id2 = Basic.businessAdd(name[1],creator[1],groupId,status);
//獲取業(yè)務(wù)線信息
Basic.businessGet(id2,name[1],creator[1],groupId,1);
//獲取所有業(yè)務(wù)線信息
Map resultmap = BusinessLineDao.getBusinessLines(1000,1,10);
SqlEnum result = (SqlEnum)resultmap.get("result");
List ListGet = (List) resultmap.get("businessLinesList");
for(int i = 0;i<2;i++){
BusinessLine temp = (BusinessLine) ListGet.toArray()[i];
String nameGet = temp.getBusinessLineName();
String creatorGet = temp.getBusinessLineCreator();
int groupIdGet = temp.getLinkGroupID();
int statusGet = temp.getBusinessLineStatus();
int total = (int) resultmap.get("count");
System.out.println("businessLinesList="+ ListGet);
System.out.println("total="+ total);
Assert.assertEquals(name[i], nameGet);
Assert.assertEquals(creator[i], creatorGet);
Assert.assertEquals(groupId, groupIdGet);
Assert.assertEquals(1, statusGet);
Assert.assertEquals(2, total);
Assert.assertEquals(SUCCESS, result);
System.out.println("獲取所有業(yè)務(wù)線結(jié)果:"+result);
}
}
}
Basic.java
package com.xingshulin.BusinessDaoTests;
import com.xingshulin.entities.BusinessLine;
import com.xingshulin.utils.SqlEnum;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.Map;
import static com.xingshulin.utils.SqlEnum.SUCCESS;
@Component
public class Basic {
@Autowired
com.xingshulin.dao.BusinessLineDao BusinessLineDao;
@Autowired
private JdbcTemplate jdbcTemplate;
com.xingshulin.entities.BusinessLine BusinessLine = new BusinessLine();
public BusinessLine getBusinessLine(String name,String creator,int groupId,int status) {
//增加業(yè)務(wù)線
BusinessLine.setBusinessLineName(name);
BusinessLine.setBusinessLineCreator(creator);
BusinessLine.setLinkGroupID(groupId);
BusinessLine.setBusinessLineStatus(status);
return BusinessLine;
}
public void businessGet(int id,String name,String creator,int groupId,int status){
Map resultmap = BusinessLineDao.getBusinessLine(id);
//獲取某id業(yè)務(wù)線的名稱乞娄、創(chuàng)建者瞬逊,所屬團隊、狀態(tài)
SqlEnum result = (SqlEnum)resultmap.get("result");
BusinessLine businessLineGet = (BusinessLine) resultmap.get("businessLine");
String nameGet = businessLineGet.getBusinessLineName();
String creatorGet = businessLineGet.getBusinessLineCreator();
int groupIdGet = businessLineGet.getLinkGroupID();
int statusGet = businessLineGet.getBusinessLineStatus();
//判斷某id業(yè)務(wù)線的名稱仪或、創(chuàng)建者确镊、所屬團隊、狀態(tài)是否正確
Assert.assertEquals(name, nameGet);
Assert.assertEquals(creator, creatorGet);
Assert.assertEquals(groupId, groupIdGet);
Assert.assertEquals(status, statusGet);
Assert.assertEquals(SUCCESS, result);
System.out.println("獲取業(yè)務(wù)線信息結(jié)果:"+result);
}
public int businessAdd(String name,String creator,int groupId,int status){
BusinessLine = getBusinessLine(name,creator,groupId,status);
SqlEnum result = BusinessLineDao.addBusinessLine(BusinessLine);
Assert.assertEquals(SUCCESS, result);
System.out.println("增加業(yè)務(wù)線結(jié)果:"+result);
Map businessMap = jdbcTemplate.queryForMap("select id from interface_business_lines where business_line_name=? and status = 1",name);
int id = (int)businessMap.get("id");
System.out.println("業(yè)務(wù)線id:"+ id);
return id;
}
}