<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # JUnit5 `@BeforeEach`注解示例 > 原文: [https://howtodoinjava.com/junit5/before-each-annotation-example/](https://howtodoinjava.com/junit5/before-each-annotation-example/) JUnit5 [`@BeforeEach`](http://junit.org/junit5/docs/current/api/org/junit/jupiter/api/BeforeEach.html)注解替換了 JUnit4 中的`@Before`注解。它用于表示應在當前類中的每個`@Test`方法之前執行**注解方法**。 ## `@BeforeEach`注解用法 使用`@BeforeEach`注解方法,如下所示: ```java @BeforeEach public void initEach(){ System.out.println("Before Each initEach() method called"); } ``` `@BeforeEach`**注解的方法不得為靜態方法**,否則它將引發運行時錯誤。 ```java org.junit.platform.commons.JUnitException: @BeforeEach method 'public static void com.howtodoinjava.junit5.examples.JUnit5AnnotationsExample.initEach()' must not be static. at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.assertNonStatic(LifecycleMethodUtils.java:73) at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.lambda$findBeforeEachMethods$2(LifecycleMethodUtils.java:54) at java.util.ArrayList.forEach(ArrayList.java:1249) at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1080) at org.junit.jupiter.engine.descriptor.LifecycleMethodUtils.findBeforeEachMethods(LifecycleMethodUtils.java:54) ``` ## `@BeforeEach`注解示例 讓我們舉個例子。 我使用了一個`Calculator`類并添加了一個`add`方法。 我將使用`@RepeatedTest`注解對其進行 5 次測試。 此注解將導致`add`測試運行 5 次。 對于每次運行的測試方法,`@BeforeEach`帶注解的方法也應每次運行。 ```java package com.howtodoinjava.junit5.examples; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.RepetitionInfo; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; import org.junit.platform.runner.JUnitPlatform; import org.junit.runner.RunWith; @RunWith(JUnitPlatform.class) public class BeforeEachTest { @DisplayName("Add operation test") @RepeatedTest(5) void addNumber(TestInfo testInfo, RepetitionInfo repetitionInfo) { System.out.println("Running test -> " + repetitionInfo.getCurrentRepetition()); Assertions.assertEquals(2, Calculator.add(1, 1), "1 + 1 should equal 2"); } @BeforeAll public static void init(){ System.out.println("BeforeAll init() method called"); } @BeforeEach public void initEach(){ System.out.println("BeforeEach initEach() method called"); } } ``` 其中`Calculator`類是: ```java package com.howtodoinjava.junit5.examples; public class Calculator { public int add(int a, int b) { return a + b; } } ``` 現在執行測試,您將看到以下控制臺輸出: ```java BeforeAll init() method called BeforeEach initEach() method called BeforeEach initEach() method called Running test -> 1 BeforeEach initEach() method called Running test -> 2 BeforeEach initEach() method called Running test -> 3 BeforeEach initEach() method called Running test -> 4 BeforeEach initEach() method called Running test -> 5 ``` 顯然,每次測試方法調用時都調用`@BeforeEach`注解的`initEach()`方法。 學習愉快! [源碼下載](https://github.com/lokeshgupta1981/Junit5Examples/tree/master/JUnit5Examples)
                  <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>

                              哎呀哎呀视频在线观看