使用@Ignore注解
package com.yiibai.junit; import static org.junit.Assert.*; import org.junit.Ignore; import org.junit.Test; public class FirstDayAtSchoolTest { FirstDayAtSchool school = new FirstDayAtSchool(); String[] bag1 = { "Books", "Notebooks", "Pens" }; String[] bag2 = { "Books", "Notebooks", "Pens", "Pencils" }; @Test public void testPrepareMyBag() { System.out.println("Inside testPrepareMyBag()"); assertArrayEquals(bag1, school.prepareMyBag()); } @Ignore @Test public void testAddPencils() { System.out.println("Inside testAddPencils()"); assertArrayEquals(bag2, school.addPencils()); } }
事實上,這輸出會發生什麼:
Inside testPrepareMyBag() My school bag contains: [Books, Notebooks, Pens]
現在,我們將從testAddPencils()方法去除@Ignore注解,修改為注釋整個類來代替。
package com.yiibai.junit; import static org.junit.Assert.*; import org.junit.Ignore; import org.junit.Test; @Ignore public class FirstDayAtSchoolTest { FirstDayAtSchool school = new FirstDayAtSchool(); String[] bag1 = { "Books", "Notebooks", "Pens" }; String[] bag2 = { "Books", "Notebooks", "Pens", "Pencils" }; @Test public void testPrepareMyBag() { System.out.println("Inside testPrepareMyBag()"); assertArrayEquals(bag1, school.prepareMyBag()); } @Test public void testAddPencils() { System.out.println("Inside testAddPencils()"); assertArrayEquals(bag2, school.addPencils()); } }
在這個測試類將不會執行,因此不顯示在控製台和JUnit視圖輸出結果: