位置:首頁 > Java技術 > JUnit教學 > 使用@Ignore注解

使用@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視圖輸出結果: