JUnit是非常流行的一个可用于单元测试的框架。
[codesyntax lang="java"]
/**
* Copyright © 1998-2016, surenpi.com All Rights Reserved.
*/
package activiti.test;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
/**
* 这是一个使用JUnit的例子,只是为了演示每个注解的使用方法,并没有实际作用。</br>
* FixMethodOrder是从4.11版本开始添加的,用于确定测试用例的执行顺序
* @author suren
* @date 2016年3月29日 上午11:08:39
*/
@FixMethodOrder
public class AllTestsTest
{
/**
* 在所有的方法开始之前运行(一次)
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception
{
System.out.println("beforeClass");
}
/**
* 在所有的方法结束后执行(一次)
* @throws java.lang.Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception
{
System.out.println("afterClass");
}
/**
* 在每个加@Test的方法执行前执行
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception
{
System.out.println("before");
}
/**
* 在每个加@Test的方法执行后执行
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception
{
System.out.println("after");
}
/**
* 所有带@Test的方法都可以认为是一个单元测试用例
*/
@Test
public void testA()
{
System.out.println("testA");
}
@Test
public void testB()
{
System.out.println("testB");
fail("Not yet implemented");
}
@Test
public void testC()
{
System.out.println("testC");
}
}
[/codesyntax] 我这里使用Maven来管理库依赖问题,下面是对应的依赖配置: [codesyntax lang="smalltalk"]
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
[/codesyntax]