简体   繁体   中英

Junit4 @Parameterized.Parameters and argument type mismatch exception

Here is my simple JUnit parametrized test, where I parse a csv with testdata, and try to give this to Parametrized.Parameters, but when I run it, my test crashes with java.lang.IllegalArgumentException: argument type mismatch GitHub link is here 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;
        }
    }
}

i got it! when i parce a csv file, i got a String [], but my constructor apply int, i must use a Integer.parseInt() in my constructor and now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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