如何将DataPoint与理论联系起来? [英] How to attach a DataPoint with a Theory?
问题描述
@DataPoints public static final Integer[] input1={1,2};
@Theory
@Test
public void test1(int input1){
}
@DataPoints public static final Integer[] input2={3,4};
@Theory
@Test
public void test2(int input2 ){
}
我希望test1与数据集input1-{1,2}一起运行,而test2与input2-{3,4}一起运行.但是目前,每个测试都使用两个数据集{1,2,3,4}进行.如何将特定的@DataPoints绑定到特定的@Theorys
I want that test1 runs with data set input1 - {1,2} and test2 runs with input2 - {3,4}. But currently each test runs with both the data sets {1,2,3,4}. How to bind specific @DataPoints to specific @Theorys
推荐答案
DataPoints应用于该类.如果您有一个采用int的@Theory方法,而您的DataPoint是一个由int组成的数组,则将使用int调用它.
DataPoints apply to the class. If you have a @Theory method which takes an int, and you have a DataPoint which is an array of ints, then it will be called with the int.
@RunWith(Theories.class)
public class TheoryTest {
@DataPoint public static int input1 = 45;
@DataPoint public static int input2 = 46;
@DataPoints public static String[] inputs = new String[] { "foobar", "barbar" };
@Theory public void testString1(String input) {
System.out.println("testString1 input=" + input);
}
@Theory public void testString2(String input) {
System.out.println("testString2 input=" + input);
}
@Theory public void test1(int input) {
System.out.println("test1 input=" + input);
}
@Theory public void test2(int input) {
System.out.println("test2 input=" + input);
}
}
这将调用test1为45& 46,而test2为45& 46.它调用带有"foobar"和"barbar"的testString1以及带有"foobar"和"barbar"的testString2.
This calls test1 with 45 & 46, and test2 with 45 & 46. It calls testString1 with "foobar" and "barbar" and testString2 with "foobar" and "barbar".
如果您确实想为不同的理论使用不同的数据集,则可以将数据包装在私有类中:
If you really want to use different data sets for different theories, you can wrap the data in a private class:
@RunWith(Theories.class)
public class TheoryTest {
public static class I1 { int i; public I1(int i) { this.i = i;} }
public static class I2 { int i; public I2(int i) { this.i = i;} }
@DataPoint public static I1 input1 = new I1(45);
@DataPoint public static I2 input2 = new I2(46);
@Theory
public void test1(I1 input) {
System.out.println("test1 input=" + input.i);
}
@Theory
public void test2(I2 input) {
System.out.println("test2 input=" + input.i);
}
}
这将用45的test1和用46的test2调用.这是可行的,但是我认为它会使代码模糊,将Test类分为两个类可能是一个更好的解决方案.
This calls test1 with 45 and test2 with 46. This works, but in my opinion, it obscures the code, and it may be a better solution to just split the Test class into two classes.
这篇关于如何将DataPoint与理论联系起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!