Java Junit使用@Parameters测试文件中的数据

Java Junit使用@Parameters测试文件中的数据,java,oracle,junit,Java,Oracle,Junit,我试图从文件中读取数据,并使用这些数据来测试程序。甚至要测试的进程也是由文件的内容决定的 Sample.txt是我从中读取数据的文件,该文件包含以下数据 testproc=sample_check('N') output=yes type=function testproc=sample_check('N') output=yes type=function testproc=sample_check('N') output=yes type=function 下面的程序尝试读取文件并将其内容

我试图从文件中读取数据,并使用这些数据来测试程序。甚至要测试的进程也是由文件的内容决定的

Sample.txt是我从中读取数据的文件,该文件包含以下数据

testproc=sample_check('N')
output=yes
type=function
testproc=sample_check('N')
output=yes
type=function
testproc=sample_check('N')
output=yes
type=function
下面的程序尝试读取文件并将其内容填充到二维字符串数组中

@RunWith(value = Parameterized.class)
public class Testdata extends Testdb {

    public String expected;
    public String actual;

    @Parameters
    public static Collection<String[]> getTestParameters() {
        String param[][] = new String [3][3];
        String temp[] = new String [3];
        int i = 0;
        int j = 0;
        try{
            BufferedReader br = new BufferedReader(new FileReader("sample.txt"));
            String strLine;
            String methodkey       = "testproc";
            String methodtypekey   = "type";
            String methodoutputkey = "output";
            String method = "";
            String methodtype = "";
            String methodoutput = "";

            //Read File Line By Line
            while ((strLine = br.readLine()) != null)
            {
                StringTokenizer st = new StringTokenizer(strLine, "=");
                while(st.hasMoreTokens())
                {
                    String key = st.nextToken();
                    String val = st.nextToken();
                    if (key.trim().equalsIgnoreCase(methodkey))
                    {
                        method = val.trim();
                        temp[j] = "SELECT " + method + " FROM dual";
                        j++;
                    }
                    else if (key.trim().equalsIgnoreCase(methodoutputkey))
                    {
                        methodoutput = val.trim();
                        temp[j] = methodoutput;
                        j++;
                    }
                    else if (key.trim().equalsIgnoreCase(methodtypekey))
                    {
                        methodtype = val.trim();
                        if (methodtype.trim().equalsIgnoreCase("function"))
                        {
                            System.out.println(i + " " + method);
                            param[i] = temp;
                            i++;
                            j = 0;
                        }
                    }
                }

            }
        }
        catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

        return Arrays.asList(param)           ;
    }

    public Testdata(String[] par) {
        this.expected = par[0];
        this.actual = par[1];
    }

    @Test
    public void test_file_data() //throws java.io.IOException
    {
        testString("Output should be"+expected , expected, actual);
    }


}

有什么建议可以让它工作吗?

你的构造函数是错误的。您需要的参数数量与
字符串[]中的项目数量相同。

public Testdata(String[] par) {
    this.expected = par[0];
    this.actual = par[1];
}
这应该是:

public Testdata(String expected, String actual, String somethingElse) {
    this.expected = expected;
    this.actual = actual;
}
见:

例如,要测试斐波那契函数,请编写:

@RunWith(参数化的.class)
公共级光纤通信测试{
@参数
公共静态列表数据(){
返回Arrays.asList(新对象[][]{
{ 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
});
}
私有int fInput;
预期的私有整数;
公共FibonacciTest(int输入,int预期){
fInput=输入;
预期=预期;
}
@试验
公开无效测试(){
assertEquals(预期的,斐波那契计算(fInput));
}
}
FibonacciTest的每个实例都将使用 两个参数构造函数和@Parameters中的数据值 方法


抛出java.lang.IllegalArgumentException的位置在哪里?另外,我认为使用ArrayList之类的集合比使用param和temp数组更容易。我怀疑param设置不正确,可能是出现问题的地方。java.lang.IllegalArgumentException:sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)的参数数量错误关于使用集合,我能否使用类似于上面字符串[][]数组的二维集合?原因是Testdata需要两个字符串值,每个testcase都需要一个数组或类似的字符串值,并且因为会有多个testcase,所以需要一个二维数组。ArrayList arrList2D=新的ArrayList(2);add(新的ArrayList());add(新的ArrayList());另外,作为旁注,您可能能够使用多态性重构if…else if…else if块。你熟悉重构吗?如果是这样,我指的是“用多态性替换条件”。不过,这只是一个旁注,可能会增加测试的复杂性……谢谢你的提示。这正是问题所在。。使用“publictestdata(stringexpected,stringactual,stringsomethingelse){this.expected=expected;this.actual=actual;}”修复了它
public Testdata(String expected, String actual, String somethingElse) {
    this.expected = expected;
    this.actual = actual;
}
@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters
    public static List<Object[]> data() {
        return Arrays.asList(new Object[][] {
                { 0, 0 }, { 1, 1 }, { 2, 1 }, { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 }
        });
    }

    private int fInput;
    private int fExpected;

    public FibonacciTest(int input, int expected) {
        fInput= input;
        fExpected= expected;
    }

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}