我有一個小的JUnit測試導出一個對象到文件系統。首先我的測試看起來像這樣單元測試是否必須有一個聲明像「assertEquals(..)」
public void exportTest {
//...creating a list with some objects to export...
JAXBService service = new JAXBService();
service.exportList(list, "output.xml");
}
通常我的測試包含斷言喜歡的assertEquals(...)。所以我改變了代碼以下
public void exportCustomerListTest() throws Exception {
// delete the old resulting file, so we can test for a new one at the end
File file = new File("output.xml");
file.delete();
//...creating a list with some objects to export...
JAXBService service = new JAXBService();
service.exportCustomers(list, "output.xml");
// Test if a file has been created and if it contains some bytes
FileReader fis = new FileReader("output.xml");
int firstByte = fis.read();
assertTrue(firstByte != -1);
}
我需要這個,或者是第一種方法就夠了嗎?我在問,因爲第一個實際上只是「測試」代碼運行,而不是測試任何結果。還是我可以依靠「合同」,如果出口方法運行沒有例外的測試通過?
感謝