[英]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.