简体   繁体   English

与@Parameters一起使用Junit来测试文件中的数据

[英]Junit with @Parameters to test data from file

I am trying to read data from a file and use the data for testing the procs. 我试图从文件中读取数据并使用数据来测试过程。 Even the proc to be tested is determined from the contents of the file. 甚至要测试的proc也是根据文件的内容确定的。

Sample.txt is the file from which I read the data, the file contains the following data 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

The below program tries to read the file and populate its contents into a 2 dimensional String array. 下面的程序尝试读取文件并将其内容填充到二维String数组中。

@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);
    }


}

I receive an error java.lang.IllegalArgumentException: wrong number of arguments 我收到一个错误java.lang.IllegalArgumentException:错误的参数数量

testString is a method that connects to the database to check if the actual value tallies with the expected result. testString是一种连接到数据库的方法,用于检查实际值是否与预期结果一致。 This takes two string values as argument. 这需要两个字符串值作为参数。

My question is how should return Arrays.asList(param) and method public Testdata(String[] par) look like? 我的问题是如何返回Arrays.asList(param)和方法public Testdata(String [] par)的样子?

I tested using this and it works fine but since I read from a file, I want to use an array which needs to be returned using return Arrays.asList 我测试使用它,它工作正常但是因为我从一个文件中读取,我想使用一个需要使用返回Arrays.asList返回的数组

return Arrays.asList(new String[][]{
            { "yes", "SELECT sample_check('N') FROM dual"},
            { "yes", "SELECT sample_check('N') FROM dual"},
            { "yes", "SELECT sample_check('N') FROM dual"}
    })           ;
}

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

Any advice on getting this working? 如何让这项工作?

Your constructor is wrong. 你的构造函数是错误的。 You need the same number of parameters that you have items in your String[] 您需要与String[]项目具有相同数量的参数

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

This should be: 这应该是:

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

See the javadoc for Parameterized : 请参阅参数化javadoc

For example, to test a Fibonacci function, write: 例如,要测试Fibonacci函数,请写:

@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));
    }
}

Each instance of FibonacciTest will be constructed using the two-argument constructor and the data values in the @Parameters method. FibonacciTest的每个实例都将使用双参数构造函数和@Parameters方法中的数据值构造。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM