zoukankan      html  css  js  c++  java
  • Java_业务测试

    代码测试

    两种方式验证代码正确

    一.主方法测试

    如果不知道如何编写测试程序,那么就根据主方法进行调用。

    范例:测试增加操作

    package xxx.test.main;
    
    import java.util.Date;
    
    import xxx.factory.ServiceFactory;
    import xxx.vo.Emp;
    
    public class TestEmpInsert {
    
        public static void main(String[] args) throws Exception {
            Emp vo = new Emp();
            vo.setEmpno(88);
            vo.setEname("james");
            vo.setJob("coder");
            vo.setHiredate(new Date());
            vo.setSal(1.2);
            vo.setComm(100.0);
            System.out.println(ServiceFactory.getIEmpServiceInstance().insert(vo));
        }
    }

    范例:测试数据查询

    package xxx.test.main;
    
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    
    import xxx.factory.ServiceFactory;
    import xxx.vo.Emp;
    
    public class EmpListSplit {
    
        public static void main(String[] args) throws Exception  {
            Map<String,Object> map = ServiceFactory.getIEmpServiceInstance().listSplit("ename", "S", 1, 10);
            List<Emp> allEmps = (List<Emp>) map.get("allEmps");
            Integer empCount = (Integer) map.get("empCount");
            System.out.println("符合查询的数据量:" + empCount);
            Iterator<Emp> iter = allEmps.iterator();
            while(iter.hasNext()) {
                System.out.println(iter.next());
            }
        }
    
    }

    利用junit测试

    只要是业务开发人员,一定要在业务开发完成之后编写junit的TestCase。首先选择要测试的接口。

    package xxx.test.junit;
    
    import static org.junit.jupiter.api.Assertions.*;
    
    import java.util.Date;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import org.junit.jupiter.api.Test;
    
    import junit.framework.TestCase;
    import xxx.factory.ServiceFactory;
    import xxx.vo.Emp;
    
    class IEmpServiceTest {
    
        @Test
        void testInsert() {
            Emp vo = new Emp();
            vo.setEmpno(8888);
            vo.setEname("james2");
            vo.setJob("FICO3");
            vo.setHiredate(new Date());
            vo.setSal(5.2);
            vo.setComm(160.0);
            try{
                TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().insert(vo));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Test
        void testUpdate() {
            Emp vo = new Emp();
            vo.setEmpno(8888);
            vo.setEname("james3");
            vo.setJob("FICO4");
            vo.setHiredate(new Date());
            vo.setSal(1.2);
            vo.setComm(160.0);
            try{
                TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().update(vo));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Test
        void testDelete() {
            Set<Integer> all = new HashSet<Integer>();
            all.add(8888);
            try{
                TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().delete(all));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Test
        void testGet() {
            try{
                TestCase.assertNotNull(ServiceFactory.getIEmpServiceInstance().get(7369));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Test
        void testList() {
            try{
                TestCase.assertTrue(ServiceFactory.getIEmpServiceInstance().list().size() > 0);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Test
        void testListSplit() {
            try {
                Map<String,Object> map = ServiceFactory.getIEmpServiceInstance().listSplit("ename", "JAMES", 1, 10);
                List<Emp> allEmps = (List<Emp>) map.get("allEmps");
                Integer empCount = (Integer) map.get("empCount");
                TestCase.assertTrue(allEmps.size() > 0 && empCount > 0);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    }

    这些代码是留给专门的业务人员使用的。

  • 相关阅读:
    Spring系列(六):Spring事务源码解析
    Spring系列(五):Spring AOP源码解析
    Spring系列(四):Spring AOP详解
    Spring系列(三):Spring IoC源码解析
    Spring IoC源码解析之getBean
    Spring IoC源码解析之invokeBeanFactoryPostProcessors
    Spring系列(二):Spring IoC应用
    HDU-1276
    Codeforces Round #410 (Div. 2)-A
    ubuntu16.04安装wps
  • 原文地址:https://www.cnblogs.com/lonske/p/8918651.html
Copyright © 2011-2022 走看看