gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:43 27 4
gpt4 key购买 nike

这是我简单的 JUnit 参数化测试,我用 testdata 解析 csv,并尝试将其提供给 Parametrized.Parameters,但是当我运行它时,我的测试因 java.lang.IllegalArgumentException:参数类型不匹配而崩溃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() ,现在它可以工作了。

关于java - Junit4 @Parameterized.Parameters 和参数类型不匹配异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35947922/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com