前一篇文章咱们整体介绍了Junit4的用法以及一些简单的测试。以前我有个疑惑,Junit4怎么把一个test case跑起来的,在test case以前和以后咱们能作些什么?html
Junit4执行顺序是怎样的?带着这些问题,我写了这篇文章,仅供参考,不对之处,盼留言指正,感激万分。前一篇文章:【原创】Junit4详解一:Junit整体介绍java
Junit4编译器在执行TestCase的过程当中利用反射机制,以便咱们能够对测试的开始过程当中进行一些预处理,如读取元数据信息,拦截异常,数据库操做等,因为Junit4默认的测试执行器是:BlockJUnit4ClassRunner,咱们以这个执行器粗略地作一些研究。在TestCase执行过程当中,主要用到如下类,BlockJUnit4ClassRunner,ParentRunner,Statement,TestRule,Description,RunNotifier,InvokeMethod.如下简单作些解释。数据库
1. BlockJUnit4ClassRunner:Junit4的默认测试执行器,它有以前版本的runner一样的行为,也就兼容了以前的runner。可是它是基于Statement,实现更加简单,容许用户在执行工做流中某个合适的点插入用户新的操做,基于这个runner的继承和重用都是能够的,这样能更加具备灵活性。它继承ParentRunner,Junit4要求,执行器的构造函数要把测试类传进来。这个类的实现了ParentRunner的runChild(真正执行测试的方法,每一个测试方法都会执行runChild),describeChild(FrameworkMethod method)(用来获取测试类的元数据信息,以及方法和类的信息),一些验证的方法,这些验证方法在ParentRunner构造的时候就会开始验证。另一个比较重要的方法是:app
methodBlock(FrameworkMethod method)ide
method里面包含当前要测试的方法。函数
这个方法的做用验证方法可否执行,而后把当前测试类的信息(当前类,测试的方法)传给InvokeMethod,以待后续测试方法的执行,接着获取当前类的元数据信息,保存起来。post
2. ParentRunner:Junit4测试执行器的基类,它提供了一个测试器所须要的大部分功能。继承它的类须要实现:测试
protected abstract List<T> getChildren();ui
protected abstract Description describeChild(T child);this
protected abstract void runChild(T child, RunNotifier notifier);
3. Statement:在运行期时,执行test case前能够插入一些用户动做,它就是描述这些动做的一个类。继承这个类要实现:
/** * Run the action, throwing a {@code Throwable} if anything goes wrong. */ public abstract void evaluate() throws Throwable;
这个方法,这个方法会前后在ParentRunner.run()和ParentRunner.runLeaf()这两个方法里面调用。另外,咱们能够自定义一个Statement,而且实现evaluate()方法。
4. TestRule:TestRule能够描述一个或多个测试方法如何运行和报告信息的接口。在TestRule中能够额外加入一些check,咱们可让一个test case失败/成功,也能够加入一些setup和cleanup要作的事,也能够加入一些log之类的报告信息。总之,跑test case以前的任何事,均可以在里面作。须要实现apply()方法。
/** * Modifies the method-running {@link Statement} to implement this * test-running rule. * @param base The {@link Statement} to be modified * @param description A {@link Description} of the test implemented in {@code base} * @return a new statement, which may be the same as {@code base}, * a wrapper around {@code base}, or a completely new Statement. */ Statement apply(Statement base, Description description);
这两个类的用法能够见后面的综合例子。
5. Description:存储着当前单个或多个test case的描述信息。这些信息跟逻辑不关,好比元数据信息等。实例化Description用Description.createTestDescription()方法。
6. RunNotifier:运行时通知器。执行Runner.run(RunNotifier runNotifier)方法时,须要传一个RunNotifier进去,这个RunNotifier是事件的管理器,它能帮助咱们监控测试执行的状况。
7. InvokeMethod:最终执行test case里面的测试方法经过这个类来作,这个类会间接调用Method.invoke()方法通知编译器执行@test方法。
@Rule public final TestRule testRule = new CommonRule();
此时,咱们就能够在咱们新构建的CommonRule类里面,的apply()方法,作一些对test的预处理,好比预处理链接数据库字符串,读取方法上元数据的信息等;
备注:就如刚所说的,真正开始测试前,咱们能够用apply()进行一些处理,一样地,咱们能够在apply()方法中,建立用户Statement,这样就能在evaluate()方法中,作一些操做。如执行脚本,日志等。
1. 获取元数据信息的顺序
@BeforeClass -> @AfterClass -> ClassRule -> @Test(拿元数据里的expect Exception) -> @Test(拿元数据里的timeout信息) -> @Before -> @After -> @Rule,
2. 注解所标注的方法执行顺序
@ClassRule(TestRule.apply()) -> @BeforeClass -> @Rule(TestRule.apply()) -> @Before -> @Test(test method1) ->@After -> if existed @Rule, then Statement.evaluate() -> @Rule(TestRule.apply()) -> @Before -> @Test(test method2) -> @After -> if existed @Rule, then Statement.evaluate() … -> @AfterClass -> if existed @ClassRule, then Statement.evaluate()
经过Statement.evaluate()执行他们的方法实体,最终执行测试方法的主体。
附录:
TestRule Statement以及注解标识的方法执行顺序代码示例
1 package com.citi.risk.services.credit.facility.impl; 2 3 import java.io.Closeable; 4 import java.io.IOException; 5 6 import org.junit.After; 7 import org.junit.AfterClass; 8 import org.junit.Before; 9 import org.junit.BeforeClass; 10 import org.junit.ClassRule; 11 import org.junit.Rule; 12 import org.junit.Test; 13 import org.junit.rules.ExpectedException; 14 import org.junit.rules.TestRule; 15 import org.junit.runner.Description; 16 import org.junit.runners.model.Statement; 17 18 /** 19 * 执行顺序以下: 默认test方法的执行顺序是随机的,没有顺序 20 * @ClassRule(TestRule.apply()) -> @BeforeClass -> @Rule(TestRule.apply()) -> @Before 21 * -> @Test(test method1) ->@After -> if existed @Rule, then Statement.evaluate() 22 * -> @Rule(TestRule.apply()) -> @Before -> @Test(test method2) -> @After 23 * -> if existed @Rule, then Statement.evaluate() … -> @AfterClass 24 * -> if existed @ClassRule, then Statement.evaluate() 25 * @author 草原战狼 26 * 27 */ 28 public class TestClass { 29 30 @Rule 31 public ExpectedException expectedException = ExpectedException.none(); 32 33 @Rule 34 public TestRule testRule = new TestRuleValueImpl(); 35 36 @Rule 37 public TestRule testRuleMethod() { 38 System.out.println(); 39 System.out.println("@Rule Method"); 40 return new TestRuleMethodImpl(); 41 } 42 @ClassRule 43 public static TestRule testClassRuleMethod() { 44 System.out.println("@ClassRule Method"); 45 return new TestRuleMethodImpl(); 46 } 47 48 static class TestRuleValueImpl implements TestRule{ 49 @Override 50 public Statement apply(Statement base, Description description) { 51 System.out.println("@Rule property--TestRuleValueImpl execute apply()"); 52 return new StatementValueImpl(base); 53 } 54 } 55 56 static class StatementValueImpl extends Statement { 57 Statement base; 58 StatementValueImpl(Statement base) { 59 this.base = base; 60 } 61 @Override 62 public void evaluate() throws Throwable { 63 System.out.println("@Rule property--StatementValueImpl execute evaluate()"); 64 base.evaluate(); 65 } 66 67 } 68 static class TestRuleMethodImpl implements TestRule{ 69 @Override 70 public Statement apply(Statement base, Description description) { 71 System.out.println("@Rule method--TestRuleMethodImpl execute apply()"); 72 return new StatementMethodImpl(base); 73 } 74 75 } 76 77 static class StatementMethodImpl extends Statement { 78 Statement base; 79 StatementMethodImpl(Statement base) { 80 this.base = base; 81 } 82 @Override 83 public void evaluate() throws Throwable { 84 System.out.println("@Rule Method--StatementMethodImpl execute evaluate()"); 85 base.evaluate(); 86 } 87 88 } 89 static class ExpensiveManagedResource implements Closeable { 90 @Override 91 public void close() throws IOException { 92 } 93 } 94 95 static class ManagedResource implements Closeable { 96 @Override 97 public void close() throws IOException { 98 } 99 } 100 101 @BeforeClass 102 public static void setUpClass() { 103 System.out.println("@BeforeClass setUpClass"); 104 myExpensiveManagedResource = new ExpensiveManagedResource(); 105 } 106 107 @AfterClass 108 public static void tearDownClass() throws IOException { 109 System.out.println("@AfterClass tearDownClass"); 110 myExpensiveManagedResource.close(); 111 myExpensiveManagedResource = null; 112 } 113 114 private ManagedResource myManagedResource; 115 private static ExpensiveManagedResource myExpensiveManagedResource; 116 117 private void println(String string) { 118 System.out.println(string); 119 } 120 121 @Before 122 public void setUp() { 123 this.println("@Before setUp"); 124 this.myManagedResource = new ManagedResource(); 125 } 126 127 @After 128 public void tearDown() throws IOException { 129 this.println("@After tearDown"); 130 this.myManagedResource.close(); 131 this.myManagedResource = null; 132 this.println(" "); 133 } 134 135 @Test 136 public void test1() { 137 this.println(" @Test test1() begin"); 138 this.println(" @Test test1() execute during evaluate()"); 139 this.println(" @Test test1() finished"); 140 } 141 142 @Test 143 public void test2() { 144 this.println(" @Test test2() begin"); 145 this.println(" @Test test2() execute during evaluate()"); 146 this.println(" @Test test2() finished"); 147 } 148 149 @Test 150 public void test3() { 151 this.println(" @Test test3() begin"); 152 String hi = " @Test test3() execute during evaluate()"; 153 expectedException.expect(Exception.class); 154 expectedException.expectMessage("ddd"); 155 this.println(hi); 156 this.println(" @Test test3() finished."); 157 } 158 }
执行的结果以下:
1 @ClassRule Method 2 @Rule method--TestRuleMethodImpl execute apply() 3 @Rule Method--StatementMethodImpl execute evaluate() 4 @BeforeClass setUpClass // 预备期结束 5 // 第一个测试方法开始到结束
6 @Rule Method 7 @Rule method--TestRuleMethodImpl execute apply() 8 @Rule property--TestRuleValueImpl execute apply() 9 @Rule property--StatementValueImpl execute evaluate() 10 @Rule Method--StatementMethodImpl execute evaluate() 11 @Before setUp 12 @Test test1() begin 13 @Test test1() execute during evaluate() 14 @Test test1() finished 15 @After tearDown 16
17 // 第二个方法开始到结束,咱们能够在apply() 和 evaluate()这两个方法作一些操做。
18 @Rule Method 19 @Rule method--TestRuleMethodImpl execute apply() 20 @Rule property--TestRuleValueImpl execute apply() 21 @Rule property--StatementValueImpl execute evaluate() 22 @Rule Method--StatementMethodImpl execute evaluate() 23 @Before setUp 24 @Test test2() begin 25 @Test test2() execute during evaluate() 26 @Test test2() finished 27 @After tearDown 28
29 // 第三个方法,这三个方法执行的顺序是随机的,固然Junit4提供了某些排序方式能够处理
30 @Rule Method 31 @Rule method--TestRuleMethodImpl execute apply() 32 @Rule property--TestRuleValueImpl execute apply() 33 @Rule property--StatementValueImpl execute evaluate() 34 @Rule Method--StatementMethodImpl execute evaluate() 35 @Before setUp 36 @Test test3() begin 37 @Test test3() execute during evaluate() 38 @Test test3() finished. 39 @After tearDown 40
41 @AfterClass tearDownClass
草原战狼淘宝小店:http://xarxf.taobao.com/ 淘宝搜小矮人鞋坊,主营精致美丽时尚女鞋,为您的白雪公主挑一双哦。谢谢各位博友的支持。
====================================================================================
====================== 以上分析仅表明我的观点,欢迎指正与交流 =========================
====================== 草原战狼博客,转载请注明出处,万分感谢 =========================
====================================================================================