編輯:目前不提供JUnit 4。JUnit異常測試
那裏嗨,
我有一個關於使用JUnit「智能」異常測試問題。在這個時候,我不喜歡這樣寫道:
public void testGet() {
SoundFileManager sfm = new SoundFileManager();
// Test adding a sound file and then getting it by id and name.
try {
SoundFile addedFile = sfm.addSoundfile("E:\\Eclipse_Prj\\pSound\\data\\Adrenaline01.wav");
SoundFile sf = sfm.getSoundfile(addedFile.getID());
assertTrue(sf!=null);
System.out.println(sf.toString());
sf = sfm.getSoundfileByName("E:\\Eclipse_Prj\\pSound\\data\\Adrenaline01.wav");
assertTrue(sf!=null);
System.out.println(sf.toString());
} catch (RapsManagerException e) {
System.out.println(e.getMessage());
}
// Test get with invalid id.
try {
sfm.getSoundfile(-100);
fail("Should have raised a RapsManagerException");
} catch (RapsManagerException e) {
System.out.println(e.getMessage());
}
// Test get by name with invalid name
try {
sfm.getSoundfileByName(new String());
fail("Should have raised a RapsManagerException");
} catch (RapsManagerException e) {
System.out.println(e.getMessage());
}
}
正如你所看到的,我需要一個try/catch塊的是應該拋出一個異常,各項功能。這似乎不是一個好方法 - 或者是否不可能減少try/catch的使用?
我會建議反對這樣做的System.out.println內部測試。 System.out和System.err傾向於混淆併產生亂碼測試輸出。 – RockyMM 2012-11-20 12:31:59