後,這是我的測試:JUnit測試代碼停止執行檢查異常
import org.junit.rules.ExpectedException;
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void testSearch() {
List<Integer> myList = Arrays.asList(new Integer[] {1, 2, 4, 6, 3, 1, 2});
exception.expect(NoSuchElementException.class);
SimpleSearch.search(myList, 5);
System.out.println("here");
exception.expect(NoSuchElementException.class);
assertEquals(1, SimpleSearch.search(myList, 22));
}
當我運行它,它說,它跑了1/1,但它不打印here
,並沒有做任何斷言或者運行SimpleSearch.search(myList, 5);
以下的任何行(發生異常之後)。
如何在捕獲異常後繼續執行它(我想在同一個testSearch
函數中執行)?
你爲什麼要繼續嗎? – developer
@javaguy因爲我想測試其餘的代碼。 – user2719875
將其移入單獨的方法。 – shmosel