Junit @BeforeClass 看不懂
Junit @BeforeClass don't understand
这是我的测试类。我尝试解析 CSV 文件并创建一个 Object[][](调用 data
)。当我尝试给 data
作为参数时,它会调用空指针。我不知道为什么会失败。当我提供硬核代码之类的数据时,它确实有效。请解释一下,我不明白
@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
public 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<Object[]>();
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; }
}
@Test
public void checkCalculator() {
final Calculator calculator = new Calculator(firstParameter, secondParameter, operation);
int result;
if (operation.equals("*")) {
result = calculator.multi();
Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
}
else if (operation.equals("+")) {
result = calculator.plus();
Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
}
else if (operation.equals("-")) {
result = calculator.minus();
Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
}
else if (operation.equals("/")) {
result = calculator.del();
Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
}
}
@Parameterized.Parameters(name = "{index}: Действие {0} {2} {1} = {3}")
public static Collection<Object[]> getTestData() {
return Arrays.asList(data
/* new Object[][]{ //<<<NULLPOINTER HERE
{2, 2, "*", 4},
{2, 0, "+" , 2},
{2, 2,"/", 1},
{0, 2,"-",-2}
}*/);
}
}
问题出在注释@BeforeClass 中,当我们使用参数化测试时,@Before 和@BeforeClass 运行不是第一次!就我而言,我需要在方法 getTestData() 中调用方法 makeData(),如下所示:
@Parameterized.Parameters(name = "{index}: Действие {0} {2} {1} = {3}")
public static Collection<Object[]> getTestData() {
makedata(); // <<<<<I NEED USE IT HERE!
return Arrays.asList(data
/* new Object[][]{ //<<<NULLPOINTER HERE
{2, 2, "*", 4},
{2, 0, "+" , 2},
{2, 2,"/", 1},
{0, 2,"-",-2}
}*/);
这是我的测试类。我尝试解析 CSV 文件并创建一个 Object[][](调用 data
)。当我尝试给 data
作为参数时,它会调用空指针。我不知道为什么会失败。当我提供硬核代码之类的数据时,它确实有效。请解释一下,我不明白
@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
public 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<Object[]>();
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; }
}
@Test
public void checkCalculator() {
final Calculator calculator = new Calculator(firstParameter, secondParameter, operation);
int result;
if (operation.equals("*")) {
result = calculator.multi();
Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
}
else if (operation.equals("+")) {
result = calculator.plus();
Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
}
else if (operation.equals("-")) {
result = calculator.minus();
Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
}
else if (operation.equals("/")) {
result = calculator.del();
Assert.assertTrue("Результат (" + result + ") не равен" + expectedResult, result == expectedResult);
}
}
@Parameterized.Parameters(name = "{index}: Действие {0} {2} {1} = {3}")
public static Collection<Object[]> getTestData() {
return Arrays.asList(data
/* new Object[][]{ //<<<NULLPOINTER HERE
{2, 2, "*", 4},
{2, 0, "+" , 2},
{2, 2,"/", 1},
{0, 2,"-",-2}
}*/);
}
}
问题出在注释@BeforeClass 中,当我们使用参数化测试时,@Before 和@BeforeClass 运行不是第一次!就我而言,我需要在方法 getTestData() 中调用方法 makeData(),如下所示:
@Parameterized.Parameters(name = "{index}: Действие {0} {2} {1} = {3}")
public static Collection<Object[]> getTestData() {
makedata(); // <<<<<I NEED USE IT HERE!
return Arrays.asList(data
/* new Object[][]{ //<<<NULLPOINTER HERE
{2, 2, "*", 4},
{2, 0, "+" , 2},
{2, 2,"/", 1},
{0, 2,"-",-2}
}*/);