前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >单元测试基本方法

单元测试基本方法

作者头像
YGingko
发布2017-12-28 13:44:43
7940
发布2017-12-28 13:44:43
举报
文章被收录于专栏:海说海说

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

public方法测试

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

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

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

代码语言:javascript
复制
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方法进行调用。具体代码示例如下:

代码语言:javascript
复制
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方法进行测试。

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

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-05-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • public方法测试
  • private方法测试
  • 小结
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档