自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

探索JUnit4擴(kuò)展:使用Rule

開發(fā) 后端
本文將使用JUnit4.7才開始引入的擴(kuò)展方式--Rule來實(shí)現(xiàn)相同的擴(kuò)展功能。使用Rule對(duì)JUnit進(jìn)行擴(kuò)展,能夠避免對(duì)默認(rèn)Runner的擴(kuò)展,為測(cè)試類添加或移除Rule十分方便,而且Rule實(shí)現(xiàn)類本身也能很方便地被復(fù)用。

在上一篇文章《探索JUnit4擴(kuò)展:擴(kuò)展Runner》中,討論了一種擴(kuò)展JUnit4的方式,即,直接修改Test Runner的實(shí)現(xiàn)(BlockJUnit4ClassRunner)。但這種方法顯然不便于靈活地添加或刪除擴(kuò)展功能。本文將使用JUnit4.7才開始引入的擴(kuò)展方式--Rule來實(shí)現(xiàn)相同的擴(kuò)展功能。(2010.12.25***更新)

1. Rule

Rule是JUnit4.7才開始提供的一種擴(kuò)展方式,它能夠替代大部分已有的Runner擴(kuò)展。JUnit包含兩種Rule Annotation:@ClassRule與@Rule。@ClassRule應(yīng)用于測(cè)試類中的靜態(tài)變量,而@Rule應(yīng)用于成員變量;相同地是,這些變量必須是TestRule接口的實(shí)例,且訪問修飾符必須為public。

在上篇博文中,對(duì)BlockJUnit4ClassRunner進(jìn)行了擴(kuò)展,被擴(kuò)展的方法是methodBlock,現(xiàn)在我們來看看該方法體中的代碼:

  1. protected Statement methodBlock(FrameworkMethod method) {  
  2. Object test;  
  3. try {  
  4. test= new ReflectiveCallable() {  
  5. @Override 
  6. protected Object runReflectiveCall() throws Throwable {  
  7. return createTest();  
  8. }  
  9. }.run();  
  10. catch (Throwable e) {  
  11. return new Fail(e);  
  12. }  
  13.  
  14. Statement statement= methodInvoker(method, test);  
  15. statement= possiblyExpectingExceptions(method, test, statement);  
  16. statement= withPotentialTimeout(method, test, statement);  
  17. statement= withBefores(method, test, statement);  
  18. statement= withAfters(method, test, statement);  
  19. statement= withRules(method, test, statement);  
  20. return statement;  

但在BlockJUnit4ClassRunner中,possiblyExpectingExceptions(),withPotentialTimeout(),withBefores()和withAfters()都已經(jīng)被標(biāo)注為過時(shí),JUnit建議使用Rule來替代這些方法的功能。

2. TestLogRule

如第1節(jié)所述,Rule Annotation要作用于TestRule接口的實(shí)例,那么就要先創(chuàng)建一個(gè)TestRule的實(shí)現(xiàn)類。

  1. public class TestLogRule implements TestRule {  
  2. private static final DateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss_SSS");  
  3. @Override 
  4. public Statement apply(Statement base, Description description) {  
  5. TestLogger testLogger = description.getAnnotation(TestLogger.class);  
  6. if (testLogger != null) {  
  7. StringBuilder log = new StringBuilder(format.format(new Date()));  
  8. log.append(" ").append(description.getClassName()).append("#")  
  9. .append(description.getMethodName()).append(": ")  
  10. .append(testLogger.log());  
  11. System.out.println(log.toString());  
  12. }  
  13. return base;  
  14. }  

如上所示,TestLogRule與上篇博文中的LoggedRunner的代碼有許多相同之處,功能則都是打印出指定的日志,每行日志又以當(dāng)時(shí)的執(zhí)行時(shí)間與完整方法名作為前綴。

3. 使用Rule的CalculatorTest

下面是新的測(cè)試類CalculatorTest,它將不使用BlockJUnit4ClassRunner的擴(kuò)展LoggedRunner作為測(cè)試執(zhí)行器,所以該類沒有使用@RunWith(LoggedRunner.class),那么在執(zhí)行該測(cè)試類時(shí)仍然會(huì)使用BlockJUnit4ClassRunner。

  1. public class CalculatorTest {  
  2. private static Calculator calculator = null;  
  3. @Rule 
  4. public TestLogRule testLogRule = new TestLogRule();  
  5. @BeforeClass 
  6. public static void createCalculator() {  
  7. calculator = new Calculator();  
  8. }  
  9. @Test 
  10. @TestLogger(log = "a simple division")  
  11. public void simpleDivide() {  
  12. int value = calculator.divide(82);  
  13. Assert.assertTrue(value == 4);  
  14. }  
  15. @Test(expected = ArithmeticException.class)  
  16. @TestLogger(log = "divided by zero, and an ArithmeticException thrown.")  
  17. public void dividedByZero() {  
  18. calculator.divide(80);  
  19. }  

與上篇博文中的CalculatorTest相比,本文中的CalculatorTest除了沒有使用LoggedRunner之外,還多了兩行代碼:

  1. @Rule 
  2. public TestLogRule testLogRule = new TestLogRule(); 

在執(zhí)行單元測(cè)試方法之前,BlockJUnit4ClassRunner會(huì)調(diào)用TestRule/TestLogRule中的apply()方法,即,會(huì)先打印出日志內(nèi)容。

4. 小結(jié)

使用Rule對(duì)JUnit進(jìn)行擴(kuò)展,能夠避免對(duì)默認(rèn)Runner的擴(kuò)展,為測(cè)試類添加或移除Rule十分方便,而且Rule實(shí)現(xiàn)類本身也能很方便地被復(fù)用。

原文鏈接:http://www.blogjava.net/jiangshachina/archive/2011/12/24/366801.html

【編輯推薦】

  1. Java進(jìn)行HTML數(shù)據(jù)采集:淺談強(qiáng)大的group正則
  2. 利用JavaMail API 解析MIME
  3. 詳細(xì)解析Java中抽象類和接口的區(qū)別
  4. Cinch和Sysmon發(fā)布 Java輔助開發(fā)工具
  5. 解讀Java環(huán)境變量配置
責(zé)任編輯:林師授 來源: Sha Jiang的博客
相關(guān)推薦

2011-12-26 10:49:27

JavaJUnitRunner

2009-06-08 19:59:09

EclipseJUnit單元測(cè)試

2009-06-08 19:57:29

EclipseJUnit4單元測(cè)試

2009-06-08 20:04:06

EclipseJUnit4單元測(cè)試

2024-06-07 09:19:00

AIjson字符串

2011-12-01 14:40:18

JUnitJava

2021-04-21 08:56:46

Java注解反射

2012-02-07 09:08:50

Feed4JUnitJava

2021-01-07 14:06:30

Spring BootJUnit5Java

2016-09-23 10:20:22

JUnit擴(kuò)展模型Extension

2014-04-24 09:49:57

Android測(cè)試異步任務(wù)

2013-01-07 13:54:17

Android開發(fā)JUnit單元測(cè)試

2023-01-06 08:06:52

Groovy類型擴(kuò)展

2011-08-05 09:09:59

英特爾云計(jì)算

2024-05-06 11:30:06

2011-08-24 14:40:50

DROP RULE中文man

2011-08-24 11:31:47

CREATE RULE中文man

2013-06-04 09:49:04

Spring單元測(cè)試軟件測(cè)試

2012-04-26 13:46:04

ibmdw

2009-05-04 09:28:36

JunitJava
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)