• JUnit 测试


    Junit 使用

    1、忽略测试方法。在使用@Test的方法上使用@Ignore,将不会对此方法进行测试

    2、测试套件

    解决的问题:

    1、对测试类进行统一测试,而不必在单独测试类上一个一个进行测试。

    使用JUnit的@RunWith以及@SuiteClassses注解,@SuiteClassses后面为待测试类的数组

    示例:

    @RunWith(Suite.class)
    @Suite.SuiteClasses({UserTest.class})  --指定要测试的类
    public class TestAll {
    
    }

    3、参数化测试

    解决问题:对同一个方法使用不同的参数进行测试。

    1. 为准备使用参数化测试的测试类指定特殊的运行器 org.junit.runners.Parameterized。
    2. 为测试类声明几个变量,分别用于存放期望值和测试所用数据。
    3. 为测试类声明一个使用注解 org.junit.runners.Parameterized.Parameters 修饰的,返回值为 java.util.Collection 的公共静态方法,并在此方法中初始化所有需要测试的参数对。
    4. 为测试类声明一个带有参数的公共构造函数,并在其中为第二个环节中声明的几个变量赋值。
    5. 编写测试方法,使用定义的变量作为参数进行测试。

    示例:

    package com.vrvwh.wh01.testSuit;
    
    import com.vrvwh.wh01.controller.Calculator;
    import org.junit.Assert;
    import org.junit.Ignore;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    /**
     * Created by Administrator on 2015/1/22.
     */
    @RunWith(Parameterized.class)
    public class ParameterTest {
        private long expected;
        private long input1;
        private long input2;
    
        public ParameterTest(int expected, int x, int y){
            this.expected = expected;
            this.input1 = x;
            this.input2 = y;
        }
    
        @Parameterized.Parameters
        public static Collection getData(){
            Object[][] object = {{3,1,2}, {0,0,0}, {-4,-1,-3}, {6,-3,9}};
            return Arrays.asList(object);
        }
    
        @Test
        public void testAdd(){
            Calculator calculator=new Calculator();
            long result=calculator.add(input1,input2);
            Assert.assertTrue(expected == result);
    
        }
    }

    注意:getData中object 数组数据顺序必须与构造函数顺序匹配

    参考:http://www.ibm.com/developerworks/cn/java/j-lo-junit4/index.html

    http://blog.csdn.net/longeremmy/article/details/9331721

  • 相关阅读:
    ERROR 1045 (28000): Access denied for user root@localhost (using password:
    MySQL: InnoDB 还是 MyISAM?
    PHP系统函数
    为什么分离数据库软件和数据库服务?
    C#索引器的作用及使用
    asp.net 中Session的运用,及抛出错误“未将对象引用设置到对象的实例”
    C#父类对象和子类对象之间的转化
    C#中属性简写原理
    c# 中Intern的作用
    C# 中ref和out的区别
  • 原文地址:https://www.cnblogs.com/tyb1222/p/4242620.html
Copyright © 2020-2023  润新知