美文网首页
SpringBoot Dao层接口单元测试

SpringBoot Dao层接口单元测试

作者: 海的那一边 | 来源:发表于2018-12-26 19:18 被阅读47次

    1.测试对象:SpringBoot Dao层中各方法测试。
    2.测试类开始前添加的注解:
    @Transactional:每个@Test测试结束后,都对数据库进行回滚,不会在数据库中留下测试数据,也有助于@Test的独立性。
    @RunWith(SpringRunner.class)、@SpringBootTest是SpringBoot单元测试默认添加的注解。后续再查下这两个注解。
    3.测试类中的单元测试用例举例:
    测试用例名称:businessAddTest,被测试方法有addBusinessLine、getBusinessLine。程序如下。
    编写思路:
    1)首先定义程序中要使用的参数。
    2)调用需要测试的方法addBusinessLine(BusinessLine)。
    3)对该方法的结果进行判断:
    Assert.assertEquals(SUCCESS, result);
    注:Assert.assertEquals();及其重载方法: a. 如果两者一致, 程序继续往下运行. b. 如果两者不一致, 中断测试方法, 抛出异常信息 AssertionFailedError 。
    4)调用需要测试的方法getBusinessLine(id),对数据库的内容进行判断,判断是否插入了这条数据,也间接测试了方法getBusinessLine(id)。其实这里也可以直接对数据库进行查询,查看是否插入了这条数据。businessGet中对数据库获取到的name、creator、groupId、status分别进行判断,看是否是创建时传入的参数。

    @Test
        public void businessAddTest() {
            String name = "hongtest001";
            String creator = "lihong";
            int groupId = 1;
            int status = 1;
    
            //增加业务线
            int id = Basic.businessAdd(name,creator,groupId,status);
    
            //获取业务线信息
            Basic.businessGet(id,name,creator,groupId,1);
    
            //增加业务线 名称重复
            BusinessLine = Basic.getBusinessLine(name,creator,groupId,status);
            SqlEnum result = BusinessLineDao.addBusinessLine(BusinessLine);
            Assert.assertEquals(DUPLICATE, result);
            System.out.println("重复增加业务线结果:"+result);
    
            //获取业务线信息
            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业务线的名称、创建者,所属团队、状态
            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业务线的名称、创建者、所属团队、状态是否正确
            Assert.assertEquals(name, nameGet);
            Assert.assertEquals(creator, creatorGet);
            Assert.assertEquals(groupId, groupIdGet);
            Assert.assertEquals(status, statusGet);
            Assert.assertEquals(SUCCESS, result);
    
            System.out.println("获取业务线信息结果:"+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("增加业务线结果:"+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("业务线id:"+ id);
            return id;
        }
    

    4.针对BusinessLineDao类中的每一个方法,分别写出每个方法的测试用例,即每一个方法的测试都以注解@Test开始。
    5.对于重复使用的程序,可以封装成类中的方法,方便调用。这里使用到的方法都放在Basic类中。在Basic类开始前添加注解@Component,在使用的时候使用@Autowired Basic Basic来实例化。
    6.添加打印,方便调试使用:System.out.println("获取业务线信息结果:"+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;
    
        //增加业务线
        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;
    
        }
    
        //根据业务线id获取业务线
        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;
        }
    
        //根据业务线id删除业务线
        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;
        }
    
        //移动业务线
        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;
        }
    
        //修改业务线名称
        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;
        }
    
        //获取所有业务线信息-分页获取
        public Map getBusinessLines(int group_id, int pageNum, int pageSize) {
            SqlEnum result = SqlEnum.SUCCESS;
            Map map =new HashMap();
            try {
                //统计status为1即未删除的的业务线个数
                int count = jdbcTemplate.queryForObject("select count(1) from interface_business_lines where group_id=? and status=1", Integer.class, group_id);
                //从数据库中获取从第pageNum开始的、pageSize大小的数据放到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;
        }
    
    //    获取所有业务线的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;
    
    //数据库回滚
    @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;
    
            //增加业务线
            int id = Basic.businessAdd(name,creator,groupId,status);
    
            //获取业务线信息
            Basic.businessGet(id,name,creator,groupId,1);
    
            //增加业务线 名称重复
            BusinessLine = Basic.getBusinessLine(name,creator,groupId,status);
            SqlEnum result = BusinessLineDao.addBusinessLine(BusinessLine);
            Assert.assertEquals(DUPLICATE, result);
            System.out.println("重复增加业务线结果:"+result);
    
            //获取业务线信息
            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;
    
            //增加业务线
            int id = Basic.businessAdd(name,creator,groupId,status);
    
            //获取业务线信息
            Basic.businessGet(id,name,creator,groupId,1);
    
            //移动业务线
            SqlEnum result = BusinessLineDao.moveBusinessLine(id,groupIdAfter);
            Assert.assertEquals(SUCCESS, result);
            System.out.println("移动业务线结果:"+result);
    
            //获取业务线信息
            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;
    
            //增加业务线1
            int id = Basic.businessAdd(name,creator,groupId,status);
    
            //获取业务线信息
            Basic.businessGet(id,name,creator,groupId,1);
    
            //增加业务线2
            int id2 = Basic.businessAdd(name2,creator,groupId,status);
    
            //获取业务线信息
            Basic.businessGet(id2,name2,creator,groupId,1);
    
            //更新业务线名称为nameAfter
            SqlEnum result = BusinessLineDao.updateBusinessLineName(id,nameAfter);
            Assert.assertEquals(SUCCESS, result);
            System.out.println("更新业务线结果:"+result);
    
            //获取业务线信息
            Basic.businessGet(id,nameAfter,creator,groupId,1);
    
            //更新业务线名称,名称与业务线2名称重复
            result = BusinessLineDao.updateBusinessLineName(id,name2);
            Assert.assertEquals(DUPLICATE, result);
            System.out.println("更新业务线结果:"+result);
    
            //获取业务线信息
            Basic.businessGet(id,nameAfter,creator,groupId,1);
    
            //获取业务线信息
            Basic.businessGet(id2,name2,creator,groupId,1);
        }
    
        @Test
        public void businessDeleteTest() {
            String name = "hongtest001";
            String creator = "lihong";
            int groupId = 1;
            int status = 1;
    
            //增加业务线1
            int id = Basic.businessAdd(name,creator,groupId,status);
    
            //获取业务线信息
            Basic.businessGet(id,name,creator,groupId,1);
    
            //删除业务线
            SqlEnum result = BusinessLineDao.deleteBusinessLine(id);
            Assert.assertEquals(SUCCESS, result);
            System.out.println("删除业务线结果:"+result);
    
            //获取业务线信息
            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;
    
            //增加业务线1
            int id = Basic.businessAdd(name[0],creator[0],groupId,status);
    
            //获取业务线信息
            Basic.businessGet(id,name[0],creator[0],groupId,1);
    
            //增加业务线2
            int id2 = Basic.businessAdd(name[1],creator[1],groupId,status);
    
            //获取业务线信息
            Basic.businessGet(id2,name[1],creator[1],groupId,1);
    
            //获取所有业务线信息
            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("获取所有业务线结果:"+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) {
            //增加业务线
            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业务线的名称、创建者,所属团队、状态
            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业务线的名称、创建者、所属团队、状态是否正确
            Assert.assertEquals(name, nameGet);
            Assert.assertEquals(creator, creatorGet);
            Assert.assertEquals(groupId, groupIdGet);
            Assert.assertEquals(status, statusGet);
            Assert.assertEquals(SUCCESS, result);
    
            System.out.println("获取业务线信息结果:"+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("增加业务线结果:"+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("业务线id:"+ id);
            return id;
    
        }
    
    }
    

    相关文章

      网友评论

          本文标题:SpringBoot Dao层接口单元测试

          本文链接:https://www.haomeiwen.com/subject/qbkklqtx.html