使用JUnit4进行单元测试

Step1:

下载JUnit4.jar,下载地址在GitHub上:https://github.com/KentBeck/junit/downloads

Step2:

导入JUnit4.jar到eclipse工程的buildpath中,这里不再赘述。

Step3:

建立单元测试类,右键——Other——找到并选择JUnit Test Case——点击Next,出现一个Config窗口如下图:

配置按图中提示进行配置后,点击Next会弹出一个选择测试方法的窗口,如下图:

点击完成后,eclipse自动生成一个测试类,以下代码是按照我自己配置生成的:

public class TestEventManager {
        @Before
	public void setUp() throws Exception {}

        @After
	public void tearDown() throws Exception {}

        @Test
	public void testListEvents() {}

        @Test
	public void testCreateAndStoreEvent() {}

}

 Step4:编写适合自己的测试方法,以下采用代码+注释的方式阐述JUnit4的使用:

package com.cugxw.reusfk.manager;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

import com.cugxw.reusfk.domain.Event;

public class TestEventManager {

	private EventManager eventManager = new EventManager();
	
	/** 
	 * 在测试代码执行之前,可能需要我们对测试类中的某些状态进行重置,以免对其他
	 * 测试用例产生影响,此操作在参考文档中称之为Fixture 
	 */
	@Before
	public void setUp() throws Exception {
		
		System.out.println("---------------------Start JUnit Test----------------------");
	}

	/** 
	 * 在测试代码执行之后执行 
	 */
	@After
	public void tearDown() throws Exception {
		
		System.out.println("---------------------End JUnit Test------------------------");
	}
	
	/** 
	 * 在所有测试方法执行之前执行该代码,一般放置比较吃资源的代码段,方法必须是static类型 
	 */
	@BeforeClass
	public static void setUpClass() throws Exception {
		System.out.println("******************* JUnit Test Initialized ******************");
	}
	
	/** 
	 * 在所有测试方法执行之后执行该代码 
	 */
	@AfterClass
	public static void tearDownClass() throws Exception {
		
		System.out.println("******************* JUnit Test Cleared ******************");
	}

	/**
	 * 测试testListEvents
	 */
	@Test
	public void testListEvents() {
		List<Event> events = eventManager.listEvents();
		
		/* 遍历一下执行结果 */
        for (int i = 0; i < events.size(); i++) {
            Event theEvent = (Event) events.get(i);
            System.out.println(
                    "Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate()
            );
        }
        /* 可以增加assert判断执行结果是否符合预期 */
        assertEquals(5, eventManager.listEvents().size());
	}

	/**
	 * 测试testCreateAndStoreEvent
	 */
	@Test
	public void testCreateAndStoreEvent() {
		eventManager.createAndStoreEvent("test", new Date());
	}
	
	/**
	 * 如果想要事先对有些目前还未实现的方法增加测试用例,可以使用Ignore注解忽略掉该方法,
	 * 那么,在执行JUnit Test的时候该方法不会提示错误。
	 */
	@Test
	@Ignore
	public void unCompleteMethod() {
		System.out.println("");
	}
	
	/**
	 * 限时测试,某些可能会出现无限等待的方法,可以为其设置超时时间
	 * @throws Exception 
	 */
	@Test(timeout = 1000)
	public void limitTimeMethod() throws Exception {
		Thread.sleep(2000);
	}

	/**
	 * 异常测试,某些方法会抛出我们指定的异常,但是如果这个方法没有抛出我们预期的异常,
	 * 这属于bug,我们需要测试出这样的方法
	 */
	@Test(expected = IOException.class)
	public void throwEceptionMethod() throws Exception {
		/* 必须抛出IOException及其子类才能执行成功 */
		throw new IOException();
	}
}
 这些知识JUnit4的基本用法,一般的项目已经够了。

猜你喜欢

转载自cug-xw.iteye.com/blog/1739369