JUnit理论问题

She*_*har 3 java junit junit4

我正在编写一个测试用例,其中我想为一个测试用例运行一个DataPoint,为第二个测试用例运行第二个DataPoint.

@RunWith(Theories.class)
public class DummyTest {

    @DataPoints
    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}
Run Code Online (Sandbox Code Playgroud)

我希望在第一个测试用例中我的第一个数据点是getFileNames方法,而第二个测试用例则应该调用getSecondFileNames数据点.任何人都可以建议这是可行的吗?

谢谢,
谢卡尔

Tim*_*rry 5

从即将推出的JUnit 4.12开始,您现在可以命名数据点集,并且只需要来自该集合的参数,例如:

@RunWith(Theories.class)
public class DummyTest {

    @DataPoints("fileNames1")
    public static String[] getFileNames() {
        return new String[] { "firstFile.txt","firstFile1.txt" };
    }

    @Theory
    public void test1(@FromDataPoints("fileNames1") String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

    @DataPoints("fileNames2")
    public static String[] getSecondFileNames() {
        return new String[] { "secondFile.txt","secondFile1.txt" };
    }

    @Theory
    public void test2(@FromDataPoints("fileNames2") String fileName) throws Exception {
        System.out.println(fileName);
        assertThat(true, is(equalTo(Boolean.TRUE)));
    }

}
Run Code Online (Sandbox Code Playgroud)

这应该完全解决你的问题:-).