繁体   English   中英

Junit4 @ Parameterized.Parameters和参数类型不匹配异常

[英]Junit4 @Parameterized.Parameters and argument type mismatch exception

这是我简单的JUnit参数化测试,我在其中解析csv与testdata,并尝试将其提供给Parametrized.Parameters,但是当我运行它时,我的测试崩溃了java.lang.IllegalArgumentException: argument type mismatch GitHub链接在这里https ://github.com/bobrutskovav/MyJunit/tree/master/src/test/java/com/myLogicTest

package com.myLogicTest;

import com.myLogic.Calculator;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

@RunWith(Parameterized.class)
public class TestJUnit {
    public int firstParameter;
    public int secondParameter;
    public String operation;
    public int expectedResult;

    public static Object[][] data;

    public TestJUnit(int firstParameter, int secondParameter, String operation, int expectedResult) {
        this.firstParameter = firstParameter;
        this.secondParameter = secondParameter;
        this.expectedResult = expectedResult;
        this.operation = operation;
    }

    // @BeforeClass

    @Test
    public void checkCalculator() {
        final Calculator calculator = new Calculator(firstParameter, secondParameter, operation);
        int result;
        switch (operation) {
            case "*":
                result = calculator.multi();
                Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
                break;
            case "+":
                result = calculator.plus();
                Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
                break;
            case "-":
                result = calculator.minus();
                Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
                break;
            case "/":
                result = calculator.del();

                Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
                break;
        }
    }

    @Parameterized.Parameters(name = "{index}: Действие {0} {2} {1} = {3}")
    public static Collection<Object[]> getTestData() {
        try {
            makeData();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return Arrays.asList(data); //<<<<Give a test data here
    }

    /* OR I CAN GIVE THIS,AND TEST WORKS.. new Object[][]{
                    {2, 2, "*", 4},
                    {2, 0, "+" , 2},
                    {2, 2,"/", 1},
                    {0, 2,"-",-2}
            }*/
    // Method to make a Object[][] for Parameterized.parameters
    public static void makeData() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\third\\IdeaProjects\\MyJunit\\src\\test\\java\\com\\myLogicTest\\datafile.csv"));

        ArrayList<Object[]> tempArray = new ArrayList<>();
        String newLine;
        Object[] oneString;
        while ((newLine = reader.readLine()) != null) {
            oneString = newLine.split(";");
            tempArray.add(oneString);
        }
        data = new Object[tempArray.size()][];
        for (int i = 0; i < data.length; i++) {
            Object[] row = tempArray.get(i);
            data[i] = row;
        }
    }
}

我知道了! 当我分区csv文件时,我得到了一个String [],但是我的构造函数应用了int,我必须在构造函数中使用Integer.parseInt(),现在它可以工作了。

暂无
暂无

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

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