0
這是我的TestClass。我嘗試解析CSV文件並創建一個Object [] [](調用data
)。當我試圖給參數data
時,它調用一個空指針。我不知道它爲什麼失敗。當我給像hardcore代碼的數據時,它真的有效。請解釋這一點,我不明白Junit @BeforeClass不明白
@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}
}*/);
}
}
NULLPOINTER HERE行似乎用/ *字符註釋。即使在運行時錯誤之前,這會給你一個編譯錯誤。 –
@RobertoLinares,不,這個評論是爲了在硬編碼模式下測試這個代碼,我只給了他一個沒有「數據」的新對象[] [],沒有更多 –