JUnit 提供注解 org.junit.Ignore 用於暫時忽略某個測試方法或者說整個類。因為有時候由於測試環境受限,並不能保證每一個測試方法都能正確運行。
1,方法級別上使用@ignore來注釋我們的測試方法,結果就是該方法在測試執行時會被跳過。測試結束后,還可以獲取詳細的統計信息,不僅包括了測試成功和測
試失敗的次數,也包括了被忽略的測試數目。
例如下面的代碼便表示由於沒有了數據庫鏈接,提示 JUnit 忽略測試方法 unsupportedDBCheck:
package test.junit4test; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; public class LinkinTest { @Ignore @Test public void test1() { Assert.assertTrue(true); } @Test public void test2() { Assert.assertTrue(true); } }

2,類級別上使用@ignore來修飾整個類。這個類中所有的測試都將被跳過。
package test.junit4test; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @Ignore public class LinkinTest { @Test public void test1() { Assert.assertTrue(true); } @Test public void test2() { Assert.assertTrue(true); } }

關於上面的忽略測試一定要小心。注解 org.junit.Ignore 只能用於暫時的忽略測試,如果需要永遠忽略這些測試,一定要確認被測試代碼不再需要這些測試方法,以免忽略必要的測試點。