How to attach a DataPoint with a Theory?

后端 未结 4 941
無奈伤痛
無奈伤痛 2021-02-18 23:59
@DataPoints public static final Integer[] input1={1,2};
@Theory
@Test
public void test1(int input1){

}

@DataPoints public static final Integer[] input2={3,4};
@Theory
         


        
4条回答
  •  太阳男子
    2021-02-19 00:28

    With JUnit 4.12 (not sure when it was introduced) it is possible to name the DataPoints and assign them to parameters (i learned it from http://farenda.com/junit/junit-theories-with-datapoints/):

        @RunWith(Theories.class)
        public class TheoriesAndDataPointsTest {
            @DataPoints("a values")
            public static int[] aValues() {
                return new int[]{1, 2};
            }
    
            @DataPoints("b values")
            public static int[] bValues() {
                return new int[]{3, 4};
            }
    
            @Theory
            public void theoryForA(@FromDataPoints("a values") int a) {
                System.out.printf("TheoryForA called with a = %d\n", a);
            }
    
            @Theory
            public void theoryForB(@FromDataPoints("b values") int a) {
                System.out.printf("TheoryForB called with b = %d\n", a);
            }
        }
    

    Output:

    TheoryForA called with a = 1
    TheoryForA called with a = 2
    TheoryForB called with b = 3
    TheoryForB called with b = 4
    

提交回复
热议问题