Modifica: non disponibile JUnit 4 in questo momento.JUnit Exception Testing
Hi there,
Ho una domanda sui test "intelligente" eccezione con JUnit. In questo momento, lo faccio in questo modo:
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());
}
}
Come potete vedere, ho bisogno di un blocco try/catch per ogni funzione che dovrebbe generare un'eccezione. Sembra non essere un buon modo per farlo - o non c'è possibilità di ridurre l'uso di try/catch?
vorrei suggerire di non farlo System.out.println all'interno di prova. System.out e System.err tendono a mixare e produrre output di test incomprensibili. – RockyMM