0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

JUNIT5でParameterizedTestを実施する

Posted at

JUnit 5(Jupiter)でのパラメータ化テスト(@ParameterizedTest)のサンプルをJavaで作成します。


例:加算メソッドのパラメータ化テスト

Calculator.java
public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}
CalculatorTest.java
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class CalculatorTest {

    private final Calculator calculator = new Calculator();

    @ParameterizedTest
    @CsvSource({
        "1, 2, 3",
        "0, 0, 0",
        "-1, 1, 0",
        "100, 200, 300"
    })
    void testAdd(int a, int b, int expected) {
        assertEquals(expected, calculator.add(a, b));
    }
}

ポイント

  • @ParameterizedTest でパラメータ化テストを宣言。
  • @CsvSource で複数の入力値と期待結果を渡します。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?