JUnit 4 全面引入了Annotation来执行我们编写的测试。
1、JUnit4并要求测试类继承TestCase父类,在一个测试类中,所有被@Test注解所修饰的public,void方法都是test case,都会被junit运行
虽然Junit4并不要求测试方法名以test开头,但是我们最好还是按照JUnit3.8的要求那样,以test作为测试方法名的开头。
import static org.junit.Assert.assertEquals; import org.junit.Test; public class CalculatorTest { @Test public void hello() { Calculator cal = new Calculator(); int result = cal.add(3, 5); assertEquals(8,result); } @Test public void world() { Calculator cal = new Calculator(); int result = cal.subtract(1,6); assertEquals(-5,result); } @Test public void testMultiply() { Calculator cal = new Calculator(); int result = cal.multiply(2, 3); assertEquals(6,result); } }
2、在JUnit4中,通过@Before注解实现与JUnit3.8中的setUp方法同样的功能,通过@After注解实现与JUnit3.8中的tearDown方法同样的功能。
import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.Before; import org.junit.Test; public class CalculatorTest { private Calculator cal; @Before public void init() { System.out.println("befor"); } @After public void destroy() { System.out.println("after"); } @Test public void hello() { Calculator cal = new Calculator(); int result = cal.add(3, 5); assertEquals(8,result); } @Test public void world() { Calculator cal = new Calculator(); int result = cal.subtract(1,6); assertEquals(-5,result); } @Test public void testMultiply() { Calculator cal = new Calculator(); int result = cal.multiply(2, 3); assertEquals(6,result); } }
3、在JUnit4中,可以使用 @BeforeClass与@AfterClass注解修饰一个public static void no-arg的方法,这样被@BeforeClass注解所修饰的方法会在所有测试方法执行前执行;被@AfterClass注解所修饰的方法会在所有测试方法执行之后执行。
import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CalculatorTest { private Calculator cal; @BeforeClass public static void globalInit() { System.out.println("global invoke"); } @AfterClass public static void globalDestroy() { System.out.println("globalDestroy invoke"); } @Before public void init() { System.out.println("befor"); } @After public void destroy() { System.out.println("after"); } @Test public void hello() { Calculator cal = new Calculator(); int result = cal.add(3, 5); assertEquals(8,result); } @Test public void world() { Calculator cal = new Calculator(); int result = cal.subtract(1,6); assertEquals(-5,result); } @Test public void testMultiply() { Calculator cal = new Calculator(); int result = cal.multiply(2, 3); assertEquals(6,result); } }
4、@Test属性timeout 希望测试在多长时间结束,超过这个时间,就表示测试失败;属性expected期望一个值,如expected = Exception.Class,期望方法抛出异常
import static org.junit.Assert.assertEquals; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class CalculatorTest { private Calculator cal; @BeforeClass public static void globalInit() { System.out.println("global invoke"); } @AfterClass public static void globalDestroy() { System.out.println("globalDestroy invoke"); } @Before public void init() { cal = new Calculator(); System.out.println("befor"); } @After public void destroy() { System.out.println("after"); } @Test public void hello() { int result = cal.add(3, 5); assertEquals(8,result); } @Test public void world() { int result = cal.subtract(1,6); assertEquals(-5,result); } @Test public void testMultiply() { int result = cal.multiply(2, 3); assertEquals(6,result); } @Test(expected = Exception.class) public void testDevide() throws Exception { cal.divide(1, 0); } }
int数组最大数测试
import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; public class LargestTest { private Largest largest; @Before public void setUp() throws Exception { largest = new Largest(); } @After public void tearDown() throws Exception { } @Test public void testGetLargest() { int[] array = {1,6,-1,-20,23,43}; int result = 0; try { result = largest.getLargest(array); } catch(Exception e) { Assert.fail("测试失败"); } Assert.assertEquals(43, result); } @Test(expected = Exception.class) public void testLargest2() throws Exception { largest.getLargest(null); } @Test(expected = Exception.class) public void testLargest3() throws Exception { largest.getLargest(new int[]{}); } }
5、@Ignore注解可以用于修饰测试类与测试方法,当修饰类时,表示忽略掉类中的所有测试方法;当修饰测试方法时,表示忽略掉此测试方法。
6、参数化测试(parameters):当一个测试类使用参数化运行器运行时,需要在类的声明处加上@RunWith(Parameterized.Class)注解,表示该类将不使用JUnit内建的运行器运行,而使用参数化运行器运行;在参数化运行类中提供参数的方法上要使用@parameters注解来修饰,同时在测试类的构造方法中为各个参数赋值(构造方法是由Junit调用的),最后编写测试类,它会根据参数的组数来运行测试多次。
import static org.junit.Assert.assertEquals; import java.util.Arrays; import java.util.Collection; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class ParametersTest { private int expected; private int input1; private int input2; private Calculator cal; @Parameters public static Collection prepareData() { Object[][] object = {{3,1,2},{-4,-1,-3},{5,2,3},{12,4,8}}; return Arrays.asList(object); } @Before public void setUp() { cal = new Calculator(); } public ParametersTest(int expected,int input1,int input2) { this.expected = expected; this.input1 = input1; this.input2 = input2; } @Test public void testAdd() { assertEquals(this.expected,cal.add(this.input1, this.input2)); } }
7、JUnit4的测试套件:使用@RunWith(Suite.class)和@Suite.SuiteClasses({})注解
import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses({CalculatorTest.class,LargestTest.class,ParametersTest.class}) public class TestAll { }
测试程序不需要任何代码,只需要在Suite.SuiteClasses中列出需要运行的测试类就行了
上述程序如果名为TestAll
还可以这样
import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) @Suite.SuiteClasses(TestAll.class) public class TestAll2 { }
在JUnit4中,如果想要同时运行多个测试,需要使用两个注解:@RunWith(Suite.class)以及@Suite.SuiteClasses(),通过这两个注解分别指定使用Suite运行器来运行测试,以及指定了运行哪些测试类,其中@Suite.SuiteClasses中可以继续指定Suite,这样JUnit会再去寻找里面的测试类,一直找到能够执行的TestCase并执行之。