溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎樣使用Rule

發(fā)布時(shí)間:2021-10-29 09:39:49 來源:億速云 閱讀:406 作者:柒染 欄目:編程語言

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎樣使用Rule,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

討論一種擴(kuò)展JUnit4的方式,即,直接修改Test Runner的實(shí)現(xiàn)(BlockJUnit4ClassRunner)。但這種方法顯然不便于靈活地添加或刪除擴(kuò)展功能。這里將使用JUnit4.7才開始引入的擴(kuò)展方式--Rule來實(shí)現(xiàn)相同的擴(kuò)展功能。

1. Rule

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

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

protected Statement methodBlock(FrameworkMethod method) {  Object test;  try {  test= new ReflectiveCallable() {  @Override protected Object runReflectiveCall() throws Throwable {  return createTest();  }  }.run();  } catch (Throwable e) {  return new Fail(e);  }   Statement statement= methodInvoker(method, test);  statement= possiblyExpectingExceptions(method, test, statement);  statement= withPotentialTimeout(method, test, statement);  statement= withBefores(method, test, statement);  statement= withAfters(method, test, statement);  statement= withRules(method, test, statement);  return statement;  }

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

2. TestLogRule

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

public class TestLogRule implements TestRule {  private static final DateFormat format = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss_SSS");  @Override public Statement apply(Statement base, 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());  }  return base;  }  }

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

3. 使用Rule的CalculatorTest

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

public class CalculatorTest {  private static Calculator calculator = null;  @Rule public TestLogRule testLogRule = new TestLogRule();  @BeforeClass public static void createCalculator() {  calculator = new Calculator();  }  @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);  }  }

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

@Rule public TestLogRule testLogRule = new TestLogRule();

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

4. 小結(jié)

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

上述就是小編為大家分享的怎樣使用Rule了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI