我建议您需要分解
testGet为多个单独的测试。各个try / catch块似乎彼此非常独立。您可能还希望将通用初始化逻辑提取到其自己的设置方法中。
一旦有了它,就可以使用JUnit4的异常注释支持,如下所示:
public class MyTest {private SoundManager sfm;@Beforepublic void setup() { sfm = new SoundFileManager();}@Testpublic void getByIdAndName() { // Test adding a sound file and then getting it by id and name. 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());}@Test(expected=RapsManagerException.class)public void getByInvalidId() { // Test get with invalid id. sfm.getSoundfile(-100);}@Test(expected=RapsManagerException.class)public void getByInvalidName() { // Test get with invalid id. sfm.getSoundfileByName(new String());}}


