現(xiàn)在已經(jīng)了解了JUnit3.8.1的使用和其基本的工作原理,JUnit 4是JUnit框架有史以來(lái)的大改進(jìn),其主要目標(biāo)便是利用Java 5的Annotation特性簡(jiǎn)化測(cè)試用例的編寫。下面同樣通過(guò)代碼來(lái)學(xué)習(xí)一下:
1.右擊該類,選擇 新建->JUnit測(cè)試用例,選擇JUnit4,setUp和tearDown方法,點(diǎn)擊下一步,選擇需要測(cè)試的方法,JUnit會(huì)自動(dòng)生成測(cè)試的代碼框架(可以看到這個(gè)代碼框架和上面的有較大的不同),手動(dòng)添加自己的測(cè)試代碼后如下:
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class HelloTest4 { //這里不需要繼承自TestCase
private Hello hello;
public HelloTest4()
{
super();
System.out.println("a new test instance...");
}
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("call before all tests...");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("call after all tests... ");
}
@Before
public void setUp() throws Exception {
//這里不需要調(diào)用super.setUp()
System.out.println("call before test...");
hello=new Hello();
}
@After
public void tearDown() throws Exception {
//這里不需要調(diào)用super.tearDown()
System.out.println("call after test... ");
}
@Test
public void testAbs() {
System.out.println("test the method abs()");
assertEquals(16, hello.abs(16));
assertEquals(11, hello.abs(-10));//在這里,會(huì)出現(xiàn)故障,應(yīng)該把左邊的參數(shù)改為10
assertEquals(0, hello.abs(0));
}
@Test
public void testDivision() {
System.out.println("test the method division()");
assertEquals(3D, hello.division(6, 2));
assertEquals(6D, hello.division(6, 1));
assertEquals(0D, hello.division(6, 0));//在這里,會(huì)出現(xiàn)故障(與3.8.1有些不同?)
}
//下面,并不是對(duì)JunitDemo類中成員函數(shù)的測(cè)試,只是演示JUnit的一些功能
//測(cè)試是否會(huì)發(fā)生期望的異常
@Test(expected=ArithmeticException.class)
public void testDiv0() {
System.out.println("test the method Div0()");
double result=100/0;
}
//測(cè)試是否超時(shí)
@Test(timeout=1)
public void testLongTimeTask()
{
System.out.println("test the method LongTimeTask()");
double d = 0;
for(int i=1; i<10000000; i++)
d+=i;
}
}
2.運(yùn)行該測(cè)試類,輸出如下:
call before all tests...
a new test instance...
call before test...
test the method abs()
call after test...
a new test instance...
call before test...
test the method division()
call after test...
a new test instance...
call before test...
test the method Div0()
call after test...
a new test instance...
call before test...
test the method LongTimeTask()
call after test...
call after all tests...
3.從上面的輸出結(jié)果可以看出,JUnit的工作原理和以前的幾乎還是沒(méi)有變的,只是讓用戶使用更簡(jiǎn)單了當(dāng)然有一個(gè)變化是3.8.1的所有測(cè)試實(shí)例是測(cè)試前全都創(chuàng)建好的,而JUnit4的測(cè)試實(shí)例是在每個(gè)測(cè)試前創(chuàng)建的。
4.當(dāng)JUnit4還有一個(gè)與以前很大的不同是引入了@BeforeClass和@AfterClass(可以在選擇setUp和tearDown的時(shí)候選擇setUpBeforeClass()和tearDownAfterClass()),setUpBeforeClass()在所有測(cè)試前調(diào)用,tearDownAfterClass()在所有測(cè)試后調(diào)用,它們不同與setUp和tearDown,在整個(gè)測(cè)試過(guò)程中只會(huì)被調(diào)用一次,這是為了能在@BeforeClass中初始化一些昂貴的資源,例如數(shù)據(jù)庫(kù)連接,然后執(zhí)行所有的測(cè)試方法,后在@AfterClass中釋放資源。
總結(jié),這里只是對(duì)JUnit對(duì)class的單元測(cè)試作了簡(jiǎn)單的討論,除此以外,JUnit還可以對(duì)JSP,Servlt,EJB等做單元測(cè)試。