here is my class that needed to test:
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;
}
here is my JUnit test class (for @Before, @After, .etc, I didn't do any thing special, just print some messages):
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");
}
}
When I run the test, it throws an exception: java.lang.NullPointerException at * .RankingTableModelTest.setFilePathAndGetFilePath(RankingTableModelTest.java:51) 51 is the number of line : test_model.setFilePath(file_path);
What is the problem with my code? Thx :)
You never instantiated test_model
.
Somewhere, you'll need test_model = new RankingTableModel();
or something similar.
You have just declared test_model:
private RankingTableModel test_model;
Not, initialized it.
private RankingTableModel test_model = new RankingTableModel();
Above line will fix your problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.