junit 使用理论进行单元测试


本文向大家介绍junit 使用理论进行单元测试,包括了junit 使用理论进行单元测试的使用技巧和注意事项,需要的朋友参考一下

示例

从JavaDoc

Theoriesrunner允许针对一组无限数据点的子集测试某种功能。

运行理论

import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class FixturesTest {

    @Theory
    public void theory(){
        //...一些断言
    }
}

带注释的方法@Theory将被TheoriesRunner视为理论。

@DataPoint批注

@RunWith(Theories.class)
public class FixturesTest {

    @DataPoint
    public static String dataPoint1 = "str1";
    @DataPoint
    public static String dataPoint2 = "str2";
    @DataPoint
    public static int intDataPoint = 2;

    @Theory
    public void theoryMethod(String dataPoint, int intData){
        //...一些断言
    }
}

每个带有注释的字段@DataPoint将用作理论中给定类型的方法参数。在上面的示例中,theoryMethod将使用以下参数运行两次:["str1", 2] , ["str2", 2]

@DataPoints批注@RunWith(Theories.class)公共类FixturesTest {

    @DataPoints
    public static String[] dataPoints = new String[]{"str1", "str2"};
    @DataPoints
    public static int[] dataPoints = new int[]{1, 2};

    @Theory
    public void theoryMethod(String dataPoint, ){
        //...一些断言
    }
}

带有@DataPoints注释的数组中的每个元素都将用作理论中给定类型的方法参数。在上面的示例中,theoryMethod将使用以下参数运行四次:["str1", 1], ["str2", 1], ["str1", 2], ["str2", 2]