使用@Ignore註解
斷續上一節的例子,瞭解如何使用@Ignore註解。在測試類FirstDayAtSchoolTest中,我們將添加@Ignore註解到testAddPencils()方法。以這種方式,我們期望這個測試方法將被忽略,不被執行。
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視圖輸出結果: