4

ファイルからデータを読み取り、そのデータを使用してプロシージャをテストしようとしています。テストする proc も、ファイルの内容から決定されます。

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

以下のプログラムは、ファイルを読み取り、その内容を 2 次元の 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);
    }


}

java.lang.IllegalArgumentException: wrong number of arguments というエラーが表示されます

testString は、データベースに接続して、実際の値が期待される結果と一致するかどうかを確認するメソッドです。これは、2 つの文字列値を引数として取ります。

私の質問は、Arrays.asList(param) とメソッド public Testdata(String[] par) をどのように返す必要があるかです。

これを使用してテストしたところ、正常に動作しましたが、ファイルから読み取ったため、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;
}

これを機能させるためのアドバイスはありますか?

4

1 に答える 1

4

あなたのコンストラクタは間違っています。アイテムの数と同じ数のパラメータが必要ですString[]

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

Parameterized の javadoc を参照してください。

たとえば、フィボナッチ関数をテストするには、次のように記述します。

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

FibonacciTest の各インスタンスは、2 つの引数のコンストラクターと @Parameters メソッドのデータ値を使用して構築されます。

于 2012-10-01T21:33:20.960 に答える