zoukankan      html  css  js  c++  java
  • 单元测试基本方法

    依照类型划分,单元测试方法可以划分为两大类。一类是针对public方法进行测试,另一类是针对private方法进行测试。

    public方法测试

    public方法和public static方法均属于public方法。public方法单元测试较简单。可分为需要Mock型和不需要Mock型。

    需要Mock型public方法单元测试可类似于Spring Service层测试

    不需要Mock型public方法单元测试可以直接构建输入数据通过Junit工具校验程序运行结果,示例如下:

    import com.alibaba.fastjson.JSON;
    import com.agoura.model.util.FileUtils;
    import com.agoura.model.util.Junit4ClassRunner;
    import com.agoura.model.utils.FileIOUtils;
    import com.agoura.model.utils.JsonUtil;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    
    import java.lang.reflect.Method;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    
    import static org.junit.Assert.assertEquals;
    
    @RunWith(Junit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath*:spring-mybatis.xml"})
    public class SimpleTest {
        private Simple simple = new Simple();
    
        @Before
        public void before() throws Exception {
        }
    
        @After
        public void after() throws Exception {
        }
    
        @Test
        public void testModel() {
            List<String> testData = FileIOUtils.readlines("/Simple/测试数据.txt");
            List<String> results = new LinkedList<>();
            for (String str : testData) {
                simple = new Simple();
                simple.init(new DataFormat(JSON.parseObject(str).getJSONObject("data")));
                Map<String, Object> result = simple.getResult();
                results.add(JSON.parseObject(JSON.toJSONString(result)).toJSONString());
    
                Map<String, Object> resultForCompany = simple.getResultForCompany();
                results.add(JsonUtil.toJSON(resultForCompany).toJSONString());
            }
            List<String> expect = FileIOUtils.readlines("/Simple/测试结果.txt");
            for (int i = 0; i < results.size(); i++) {
                assertEquals(expect.get(i), results.get(i));
            }
        }
    }
    

    private方法测试

    private方法是类内部方法,不能直接在外部调用。对private方法进行测试时需要想办法将其变为可以在外部进行调用。利用反射机制刚好可以实现对被测试类中private方法进行调用。具体代码示例如下:

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.test.context.ContextConfiguration;
    
    import java.lang.reflect.Method;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Map;
    
    import static org.junit.Assert.assertEquals;
    
    @RunWith(Junit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath*:spring-mybatis.xml"})
    public class SimpleTest {
        private Simple simple = new Simple();
    
        @Before
        public void before() throws Exception {
        }
    
        @After
        public void after() throws Exception {
        }
    
        /**
         * Method: getLimit(int score1, Integer score2)
         */
        @Test
        public void testGetLimit() throws Exception {
            try {
                Method method = Simple.class.getDeclaredMethod("getLimit", Integer.class, Integer.class);       //反射得到被测试方法
                method.setAccessible(true);         //将被测试方法设置为可调用
                int limit = (int) method.invoke(simple, null, 0);
                assertEquals(0, limit);
                limit = (int) method.invoke(simple, 600, null);
                assertEquals(0, limit);
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    } 
    

    小结

    虽然无论是public方法还是private方法都可以对其进行测试,但是public方法测试更为简单便利,所以尽量减少对private方法进行测试。

    在开发过程中尽量对方法进行细分,将一个方法合理细分成多个方法,一般按照功能划分,使每个方法功能都尽量简单单一。这样测试时构造数据也相对较容易,便于对单一功能方法进行测试。

  • 相关阅读:
    获取 .properties 配置文件中的值
    struts1.2分页实现思路
    Apache与Tomcat的3种连接方式分析
    HTML 中的rel
    自动刷新本页,定时刷新页面,定时转发
    HTML 中的rel 用法
    <globalforwards>标签
    struts1.2中从一个action跳转到另外一个action的方法
    分页传值的话,可以直接用<html:link>
    DIV+CSS效果(实现平滑投票效果等)
  • 原文地址:https://www.cnblogs.com/hthuang/p/6902983.html
Copyright © 2011-2022 走看看