<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國際加速解決方案。 廣告
                # JUnit 參數化測試示例 > 原文: [https://howtodoinjava.com/junit/how-to-write-parameterized-testcases-with-junit-4/](https://howtodoinjava.com/junit/how-to-write-parameterized-testcases-with-junit-4/) 在本 [JUnit 教程](https://howtodoinjava.com/junit-5-tutorial/)中,學習創建和執行 junit 參數化測試。 參數化測試是正常測試,它使用不同的測試參數反復執行。 它可以幫助開發人員在使用不同輸入類型執行相同測試以測試函數健壯性,以及可能的函數邊界上面節省時間。 ## 1\. JUnit Maven 依賴項 下面是 **maven 依賴項**,我們應該在測試示例代碼之前添加到您的 maven 項目中。 ```java <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit-dep</artifactId> <version>4.11</version> <scope>test</scope> </dependency> ``` ## 2\. 具有構造器參數的 JUnit 參數化測試 參數化測試使用`@RunWith`注解以及`@Parameters`注解來饋送輸入。 #### 2.1 要測試的類 下面是測試類,我們將為其編寫測試用例。 ```java package corejava.test.junit; public final class MathUtils { //Return square of a function public static int square(final int number) { return number * number; } } ``` #### 2.2 參數化測試 讓我們為上述數學工具類編寫參數化測試。 ```java package corejava.test.junit; import java.util.Arrays; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class JunitTestsWithParameters { // @Parameters annotation marks this method as parameters provider @Parameters(name = "Run #Square of : {0}^2={1}") public static Iterable<Object []> data() { return Arrays.asList(new Object[][] { { 1, 1 }, { 2, 4 }, { 3, 19 }, { 4, 16 }, { 5, 25 } }); } // Our two parameters private final int input; private final int resultExpected; // Constructor is initialized with one set of parameters every time public JunitTestsWithParameters(final int input, final int result) { this.input = input; this.resultExpected = result; } @Test public void testUserMapping() { // You can use here assert also Assert.assertEquals(resultExpected, MathUtils.square(input)); } } ``` 請注意: 1. 我們必須按照給定的方式聲明參數。 2. 參數傳遞給類的構造器以設置變量,因此可以在測試用例中使用。 3. 參數類的返回類型為“`List[]`”,要使用的數據類型已限于字符串或原始值 現在檢查程序輸出。 ![JUnit4 test execution](https://img.kancloud.cn/e8/01/e801b2a32126534223c6a2bdfbd4983e_1042x178.png "JUnit4 test execution") 測試執行結果 ## 3\. 帶有字段注入的 JUnit 參數化測試 為了傳遞參數進行測試,我們可以通過字段注入傳遞參數,而不是通過構造器傳遞參數。 在這種方法中,我們聲明確切的字段數作為輸入參數。 每個字段一個參數。 讓我們通過場注入重新測試我們的`MathUtils`類。 請注意,我們如何用`@Parameter`注解的字段替換構造器。 ```java import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class JunitTestsWithFieldInjection { @Parameters(name = "Run #Square of : {0}^2={1}") public static Iterable<Object[]> data() { return Arrays.asList(new Object[][] { { 1, 1 }, { 2, 4 }, { 3, 9 }, { 4, 16 }, { 5, 25 } }); } @Parameter(value = 0) public int input; @Parameter(value = 1) public int resultExpected; @Test public void testSquare() { Assert.assertEquals(resultExpected, MathUtils.square(input)); } } ``` #### 3.1 單個字段注入 如果僅要注入一個字段,則無需在`@Parameter`注解中放置`value`屬性。 默認值始終為“`value = 0`”。 ```java import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class JunitTestsWithParameters { @Parameters(name = "Argument number {0} is positive") public static Iterable<Object[]> data() { return Arrays.asList(new Object[][] { { 0 }, { 1 }, { 2 }, { 3 }, { 4 } }); } @Parameter public int input; @Test public void testPositiveNumber() { Assert.assertEquals(true, input >= 0); } } ``` 在本文中,我們學習了如何創建參數化測試,并使用不同的參數集運行測試的多次迭代。 它有助于測試帶有參數的方法。 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看