软件测试技术之单元测试基本方法

发表于:2021-11-15 09:31

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:YGingko    来源:博客园

  依照类型划分,单元测试方法可以划分为两大类。一类是针对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方法进行测试。
  在开发过程中尽量对方法进行细分,将一个方法合理细分成多个方法,一般按照功能划分,使每个方法功能都尽量简单单一。这样测试时构造数据也相对较容易,便于对单一功能方法进行测试。

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号