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组没有单元测试