針對不同Annotation提供了一些說明

@BeforeClass :- Run only 1 time before all the test cases of class. Can be used to initialize resources.

@AfterClass :- Run only 1 time after all the test cases of class. Can be used to clean up the resources.

@Before :- Run annotated method every time individual test case is executed but run before the test case.

@After :- Run annotated method every time individual test case is executed but run after the test case

@BeforeClass和@AfterClass的method需要static。

MathCacTest1.java

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 MathCacTest1 {

@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("@BeforeClass setUpBeforeClass()") ;
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("@AfterClass tearDownAfterClass()") ;
}
@Before
public void setUp() throws Exception {
System.out.println("@Before setUp()") ;
}
@After
public void tearDown() throws Exception {
System.out.println("@After tearDown()") ;
}
@Test
public void test() {
System.out.println("@Test test()") ;
}
@Test
public void test1() {
System.out.println("@Test test1()") ;
}
}

顯示結果如下:

@BeforeClass setUpBeforeClass()

@Before setUp()

@Test test()

@After tearDown()

@Before setUp()

@Test test1()

@After tearDown()

@AfterClass tearDownAfterClass()

加入@Ignore後,則不會跑此測試程式

@Ignore("Not ready to run")
@Test
public void test1() {
System.out.println("@Test test1()") ;
}

加入@Test(timeout = 1000)後,可以針對程式跑的時間設定,超過1000毫秒後程式會丟出Execption,如下所示(無窮迴圈)

@Test(timeout = 1000) 
public void infinity() { 
while (true); 
} 

加入@Test(expected = ArithmeticException.class)若為此Exception則Junit會by pass,否則會跳出java.lang.ArithmeticException: / by zero的訊息。

@Test(expected = ArithmeticException.class) 
public void divisionWithException() { 
int i = 100/0;
}

如果想要不同組合的單元測試可以利用@RunWith和@Suite

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
MathCacTest1.class,
MathCacTest2.class
})

public class JunitTestWithSuite {
}

跑出的結果如下所示:

@BeforeClass setUpBeforeClass()

@Before setUp()

@Test test()

@After tearDown()

@Before setUp()

@Test test1()

@After tearDown()

@AfterClass tearDownAfterClass()

MathCacTest2 @BeforeClass setUpBeforeClass()

MathCacTest2 @Before setUp()

MathCacTest2 @Test test()

MathCacTest2 @After tearDown()

MathCacTest2 @Before setUp()

MathCacTest2 @Test test1()

MathCacTest2 @After tearDown()

MathCacTest2 @AfterClass tearDownAfterClass()

若要測試多組參數的情況下Junit提供了@Parameters的應用

MathCacParameterTest.java

在@Parameters後name預設是index,:後面是nameString(add),{0}代表的是第0個欄位,{1}代表的是第1個欄位,以下依此類推,透過constructor的方式可以測試多組參數。

import static org.junit.Assert.*;

import java.util.Arrays;

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 org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value = Parameterized.class)
public class MathCacParameterTest {

private int num1 ;
private int num2 ;
private int expectedValue ;

public MathCacParameterTest(int num1,int num2, int expectedValue){
this.num1=num1 ;
this.num2=num2 ;
this.expectedValue=expectedValue;
}
//Declares parameters here
@Parameters(name = "{index}: add({0}+{1})={2}")
public static Iterable<Object[]> data1() {
return Arrays.asList(new Object[][] { 
{ 3, 5, 8 }, 
{ 6, 4, 10 }, 
{ 6, 2, 9 }, //wrong
{ 1, 1, 2 } 
});
}
@Test
public void test_add() {
assertEquals(expectedValue,new MathCac().add(num1, num2));
}
}