<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # JUnit 測試監聽器– JUnit `RunListener`示例 > 原文: [https://howtodoinjava.com/junit/how-to-add-listner-in-junit-testcases/](https://howtodoinjava.com/junit/how-to-add-listner-in-junit-testcases/) 監聽器通常可以幫助監聽我們感興趣的事件。 這可能有幾個原因。 例如,我們添加了監聽器以添加特定的日志,在 Java GUI 編程中處理 UI 事件等。 JUnit 還支持通過`RunListener`類執行測試時添加監聽器。 該監聽器可用于從改進日志記錄到測試特定邏輯的各種目的。 ## 1\. JUnit `RunListener`示例 #### 1.1 JUnit 測試類 我們僅在下面舉例說明兩個測試類。 我們將監視為這些類編寫的測試而打印的日志。 ```java package com.howtodoinjava.junit; import junit.framework.Assert; import org.junit.Test; public class TestFeatureOne { @Test public void testFirstFeature() { Assert.assertTrue(true); } } ``` ```java package com.howtodoinjava.junit; import junit.framework.Assert; import org.junit.Ignore; import org.junit.Test; public class TestFeatureTwo { @Test public void testSecondFeature() { Assert.assertTrue(true); } @Test @Ignore public void testSecondFeatureIngored() { Assert.assertTrue(true); } } ``` #### 1.2 JUnit 測試監聽器 讓我們編寫運行監聽器。 該監聽器將擴展 JUnit 提供的`RunListener`類。 > 我們可以完全不包含任何方法來覆蓋任何數量的方法`RunListener`類。 ```java package com.howtodoinjava.junit.suite; import org.junit.runner.Description; import org.junit.runner.Result; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunListener; public class ExecutionListener extends RunListener { /** * Called before any tests have been run. * */ public void testRunStarted(Description description) throws java.lang.Exception { System.out.println("Number of tests to execute : " + description.testCount()); } /** * Called when all tests have finished * */ public void testRunFinished(Result result) throws java.lang.Exception { System.out.println("Number of tests executed : " + result.getRunCount()); } /** * Called when an atomic test is about to be started. * */ public void testStarted(Description description) throws java.lang.Exception { System.out.println("Starting execution of test case : "+ description.getMethodName()); } /** * Called when an atomic test has finished, whether the test succeeds or fails. * */ public void testFinished(Description description) throws java.lang.Exception { System.out.println("Finished execution of test case : "+ description.getMethodName()); } /** * Called when an atomic test fails. * */ public void testFailure(Failure failure) throws java.lang.Exception { System.out.println("Execution of test case failed : "+ failure.getMessage()); } /** * Called when a test will not be run, generally because a test method is annotated with Ignore. * */ public void testIgnored(Description description) throws java.lang.Exception { System.out.println("Execution of test case ignored : "+ description.getMethodName()); } } ``` ## 2\. JUnit 監聽器執行 現在,讓我們運行測試并觀察監聽器的輸出。 ```java package com.howtodoinjava.junit.suite; import org.junit.runner.JUnitCore; import com.howtodoinjava.junit.TestFeatureOne; import com.howtodoinjava.junit.TestFeatureTwo; public class ExecuteWithRunListener { public static void main(String[] args) { JUnitCore runner = new JUnitCore(); <strong>//Adding listener here</strong> runner.addListener(new ExecutionListener()); runner.run(TestFeatureOne.class, TestFeatureTwo.class); } } ``` 程序輸出。 ```java Number of tests to execute : 3 Starting execution of test case : testFirstFeature Finished execution of test case : testFirstFeature Starting execution of test case : testSecondFeature Finished execution of test case : testSecondFeature Execution of test case ignored : testSecondFeatureIngored Number of tests executed : 2 ``` 顯然,添加監聽器可通過改進的日志記錄支持對測試執行提供額外的控制。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看