这是我需要测试的课程:
public class RankingTableModel {
/**
* filePath stores the path of a csv file.
*/
private String filePath = "";
/**
* table data represents all the data in the table cells.
*/
private Object[][] table_data;
public String getFilePath () {
return filePath;
}
public void setFilePath (String filePath) {
this.filePath = filePath;
}
这是我的 JUnit 测试类(对于@Before、@After、.etc,我没有做任何特别的事情,只是打印一些消息):
public class RankingTableModelTest {
private static final Object[][] data = {
{"US", new Integer(1), new Integer(2), new Integer(2),
new Integer(3)},
{"UK", new Integer(2), new Integer(2), new Integer(1),
new Integer(2)},
{"CHN", new Integer(3), new Integer(1), new Integer(3),
new Integer(1)},
};
private static final String file_path = "test";
private RankingTableModel test_model;
@Test
public void setFilePathAndGetFilePath() {
System.out.println("Testing setFilePath and getFilePath.");
test_model.setFilePath(file_path);
assertEquals(test_model.getFilePath(),"test");
}
}
当我运行测试时,它会引发异常: java.lang.NullPointerException at * .RankingTableModelTest.setFilePathAndGetFilePath(RankingTableModelTest.java:51) 51 是行数: test_model.setFilePath(file_path);
我的代码有什么问题?谢谢 :)