JUnit是一個Java語言的單元測試框架。它由Kent Beck和Erich Gamma創建,逐漸成爲源於Kent Beck的sUnit的xUnit家族中最爲成功的一個。html
測試用例不是用來證實你(的邏輯)是對的,而是用來證實你(的斷言)沒有錯。java
最大的不一樣是junit4基本用註解實現,更加靈活。git
添加進入maven的pom.xml的依賴中。設置scope爲test;github
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
import org.junit.*; public class HelloTest { @Test public void testAdd(){ Assert.assertEquals("test add", 3, 1 + 2); System.out.print("Test add Ok"); } }
public class FlowTest { @BeforeClass public static void init(){ System.out.println("test class before"); } @AfterClass public static void destory(){ System.out.println("test class after"); } @Before public void beforeTest(){ System.out.println("before test"); } @After public void afterTest(){ System.out.println("after test"); } @Test public void testAdd(){ Assert.assertEquals("test add", 3, 1 + 2); System.out.println("test add ok"); } @Test public void testSub(){ Assert.assertEquals("test subtraction", 3, 4 - 1); System.out.println("test sub ok"); } }
運行結果數據庫
test class before before test test add ok after test before test test sub ok after test test class after
public class TimeOutTest { @Test(timeout = 2000) public void testTimeOut(){ while (true){ System.out.println("I'm running!"); try { Thread.sleep(1000 *1); } catch (InterruptedException ignore) { } } } @Test(expected = ArithmeticException.class) public void testException(){ Assert.assertEquals(3,6/0); } }
運行結果數組
I'm running! I'm running! I'm running! org.junit.runners.model.TestTimedOutException: test timed out after 2000 milliseconds at java.lang.Thread.sleep(Native Method) at TimeOutTest.testTimeOut(TimeOutTest.java:15) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ...
咱們想運行全部的測試類的測試方法,難道咱們只能一個一個運行每個測試類麼,這多累啊。幸虧,junit4給咱們提供了一種方式一次運行全部的測試類-測試套件
。使用例子以下:bash
import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({FlowTest.class,TimeOutTest.class}) public class SuiteTest { }
不少時候,咱們須要對一個測試,輸入多組測試用例來驗證代碼的正確性。在junit4中,咱們不須要編寫n個測試方法。示例以下:框架
import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import java.util.Arrays; import java.util.Collection; @RunWith(Parameterized.class) public class ParamsTest { private int expected; private int input1; private int input2; public ParamsTest(int expected, int input1, int input2){ this.expected = expected; this.input1 = input1; this.input2 = input2; } @Parameterized.Parameters public static Collection<Object[]> params(){ return Arrays.asList(new Object[][]{ {3,2,1}, {4,1,4} }); } @Test public void testAdd(){ Assert.assertEquals("add function",this.expected,this.input1 + this.input2); } }
運行結果maven
java.lang.AssertionError: add function Expected :4 Actual :5 <Click to see difference> at org.junit.Assert.fail(Assert.java:88) at org.junit.Assert.failNotEquals(Assert.java:834) at org.junit.Assert.assertEquals(Assert.java:645) at ParamsTest.testAdd(ParamsTest.java:34) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ...
第一組測試經過,第2組沒有單元測試