4

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.

Sample.txt is the file from which I read the data, the file contains the following data

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.

@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

testString is a method that connects to the database to check if the actual value tallies with the expected result. This takes two string values as argument.

My question is how should return Arrays.asList(param) and method public Testdata(String[] par) look like?

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

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?

6
  • where is java.lang.IllegalArgumentException being thrown? Commented Oct 1, 2012 at 17:41
  • Also, rather than using arrays for param and temp, I think it would be easier to use a Collection such as ArrayList. I have a suspicion that param isn't being set correctly and could be where the trouble is happening. Commented Oct 1, 2012 at 17:57
  • java.lang.IllegalArgumentException: wrong number of arguments at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) Regarding using a Collection, will I be able to use 2-dimensional collection similar to the String[][] array above? The reason is Testdata requires two string values which needs to be an array or similar for each testcase and since there would be multiple testcases, it requires a 2-dimensional array. Commented Oct 1, 2012 at 18:16
  • ArrayList<ArrayList> arrList2D = new ArrayList<ArrayList>(2); arrList2D.add(new ArrayList()); arrList2D.add(new ArrayList()); Commented Oct 1, 2012 at 18:20
  • Also, just as a side note, you may be able to refactor your if...else if...else if block with polymorphism. Are you familiar with refactoring? If so, I'm referring to "Replace Conditional with Polymorphism". That's just a side note, though, and might just add complexity to your test... Commented Oct 1, 2012 at 18:24

1 Answer 1

4

Your constructor is wrong. You need the same number of parameters that you have items in your 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:

For example, to test a Fibonacci function, write:

@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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the tip. That was exactly the issue .. Using "public Testdata(String expected, String actual, String somethingElse) { this.expected = expected; this.actual = actual; }" fixed it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.