断续上一节的例子,了解如何使用@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视图输出结果: