亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

[轉]探索JUnit4擴展:深入Rule

系統 3036 0
轉載自:
探索JUnit4擴展:深入Rule

本文是"探索JUnit4擴展"系列中的第三篇,將進一步探究Rule的應用,展示如何使用Rule來替代@BeforeClass,@AfterClass,@Before和@After的功能。(2012.01.04最后更新)

在本系列的第二篇 《探索JUnit4擴展:應用Rule》 中提到,可以使用Rule替代現有的大部分Runner擴展,而且也不提倡對Runner中的withBefores(),withAfters()等方法進行擴展。本文將介紹如何使用Rule去實現@Before,@After和@BeforeClass的相同功能。

1. BaseRule
??? 首先要創建一個較通用的TestRule實現BaseRule,它會釋放出兩個擴展點,一個在執行測試方法之前,before();另一個在執行測試方法之后after()。下面是該類的代碼,

public ? abstract ? class ?BaseRule? implements ?TestRule?{

????@Override
????
public ?Statement?apply(Statement?base,?Description?description)?{
????????
return ? new ?RuleStatement(base,?description);
????}

????
private ? class ?RuleStatement? extends ?Statement?{

????????
private ?Statement?base? = ? null ;

????????
private ?Description?description? = ? null ;

????????
private ?RuleStatement(Statement?base,?Description?description)?{
????????????
this .base? = ?base;
????????????
this .description? = ?description;
????????}

????????@Override
????????
public ? void ?evaluate()? throws ?Throwable?{
????????????before(base,?description);
????????????
try ?{
????????????????base.evaluate();
????????????}?
finally ?{
????????????????after(base,?description);
????????????}
????????}
????}

????
protected ? void ?before(Statement?base,?Description?description)? throws ?Throwable?{

????}

????
protected ? void ?after(Statement?base,?Description?description)?{

????}
}

如果對JUnit4的源代碼略有認知,可能會發現BaseRule與JUnit4提供的TestRule實現ExternalResource代碼相似。關鍵的不同之處是,BaseRule中的before()與after()方法都提供了Statement與Description類型的參數,這使得它能夠完成更復雜的工作。

2. CalculatorTest
??? 本文使用的CalculatorTest將不使用@BeforeClass,@Before和@After,而會創建兩個BaseRule的實例:一個用于替代@BeforeClass和@AfterClass(本系列目前還未使用過@AfterClass),另一個則替代@Before和@After。

public ? class ?CalculatorTest?{

????
private ? static ? final ?DateFormat?format? = ? new ?SimpleDateFormat( " yyyy-MM-dd_HH:mm:ss_SSS " );

????
private ? static ?Calculator?calculator? = ? null ;

????@ClassRule
????
public ? static ?BaseRule?classRule? = ? new ?BaseRule()?{

????????
protected ? void ?before(Statement?base,?Description?description)? throws ?Throwable?{
????????????calculator?
= ? new ?Calculator();
????????};
????};

????@Rule
????
public ?BaseRule?rule? = ? new ?BaseRule()?{

????????
protected ? void ?before(Statement?base,?Description?description)? throws ?Throwable?{
????????????printBeforeLog(description);
????????};

????????
protected ? void ?after(Statement?base,?Description?description)?{
????????????printAfterLog(description);
????????};

????????
private ? void ?printBeforeLog(Description?description)?{
????????????TestLogger?testLogger?
= ?description.getAnnotation(TestLogger. class );
????????????
if ?(testLogger? != ? null )?{
????????????????StringBuilder?log?
= ? new ?StringBuilder(format.format( new ?Date()));
????????????????log.append(
" ? " ).append(description.getClassName()).append( " # " )
????????????????????????.append(description.getMethodName()).append(
" :? " )
????????????????????????.append(testLogger.log());
????????????????System.out.println(log.toString());
????????????}
????????}

????????
private ? void ?printAfterLog(Description?description)?{
????????????StringBuilder?log?
= ? new ?StringBuilder(format.format( new ?Date()));
????????????log.append(
" ? " ).append(description.getClassName()).append( " # " )
????????????????????.append(description.getMethodName()).append(
" ?end " );
????????????System.out.println(log.toString());
????????}
????};

????@Test
????@TestLogger(log?
= ? " a?simple?division " )
????
public ? void ?simpleDivide()?{
????????
int ?value? = ?calculator.divide( 8 ,? 2 );
????????Assert.assertTrue(value?
== ? 4 );
????}

????@Test(expected?
= ?ArithmeticException. class )
????@TestLogger(log?
= ? " divided?by?zero,?and?an?ArithmeticException?thrown. " )
????
public ? void ?dividedByZero()?{
????????calculator.divide(
8 ,? 0 );
????}
}

值得注意的是,classRule是靜態變量,它使用@ClassRule Annotation,將替代@BeforeClass和@AfterClass;而rule是成員變量,它使用@Rule Annotation,將替代@Before和@After。與 之前文章 不同的是,此處不僅會在執行測試方法之前打印指定內容的日志(printBeforeLog()),還會在執行測試方法之后打印一條固定格式的日志(printAfterLog()),用于指示該測試方法已經執行完畢了。

3. 小結
??? 使用Rule可以替代絕大部分的Runner擴展,而且特定的Rule實現可以被復用,也易于添加或移除Rule實例,這些都大大地提高了靈活性。值得注意地是,本文雖然使用Rule代替了@BeforeClass,@AfterClass,@Before和@After的功能,但并不意味著就應當這么做。就我個人所想,將傳統的Fixture功能交由@BeforeClass,@AfterClass,@Before和@After實現,仍然是一種不錯的選擇。

[轉]探索JUnit4擴展:深入Rule


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 亚洲欧洲国产成人精品 | 国产精品伦理久久久久久 | 男人私人影院免费看视频 | 欧美狠狠入鲁的视频极速 | 四虎激情 | 91精品久久久久含羞草 | 色人阁婷婷 | 综合亚洲欧美日韩一区二区 | 国产一精品一aⅴ一免费 | 欧美国产精品 | 国产高清对白国产露脸91 | 四虎永久在线精品国产免费 | 成人区在线观看免费视频 | 色综合久久中文综合网 | 视频精品一区二区三区 | 亚欧毛片基地国产毛片基地 | 亚洲另类中文字幕 | 999奇米| 久久精品免视看国产陈冠希 | 老子午夜影院 | 久久久久久久久a免费 | 欧美做爱毛片 | 亚洲精品久久中文字幕 | 国产高清自拍视频 | 伊人婷婷色香五月综合缴缴情 | 午夜探花在线观看 | 天天综合在线观看 | ww亚洲ww在线观看国产 | 国产午夜爽爽窝窝在线观看 | 深夜影院在线视频观看 | 男人的天堂黄 | 四虎影在永久地址在线观看 | 夜夜超b天天 | 真人一级毛片免费观看视频 | 精品久久亚洲 | 国产视频在线观看福利 | 久草国产视频 | 久久精品久久久久 | 国产aaa级一级毛片 国产aaa毛片 | 欧美日本一二三区 | 51国产午夜精品免费视频 |