JUnit 5: How to write test cases in enum

JUnit 5 provides ** @ParameterizedTest ** annotations that allow you to pass values from various argument sources to test methods as arguments. In this article, I would like to show you how to easily write many test cases by using one of these argument sources, ** @EnumSource **.

Preparation

If you use ** @ParameterizedTest **, you need to explicitly add junit-jupiter-params in addition to junit-jupiter-engine as project dependencies. Also, in this sample code, AssertJ was used to verify the test results.

pom.xml

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.5.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-params</artifactId>
    <version>5.5.1</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.13.1</version>
    <scope>test</scope>
</dependency>

Subject to test

Here, we will test the JDK's BigInteger # gcd () method. This method returns the greatest common divisor of itself and the given arguments.

Write a test case

Let's just describe the combination of two input values and the correct answer as an enum type constant.

enum GcdTestCase {
    THIS_IS_BIGGER(25, 10, 5),
    OTHER_IS_BIGGER(14, 21, 7),
    BOTH_ARE_EVEN(12, 8, 4),
    BOTH_ARE_ODD(27, 45, 9),
    BOTH_ARE_ZERO(0, 0, 0),
    BOTH_ARE_ONE(1, 1, 1),
    THIS_IS_ZERO(0, 3, 3),
    OTHER_IS_ZERO(4, 0, 4),
    THIS_IS_DIVISOR(3, 12, 3),
    OTHER_IS_DIVISOR(20, 4, 4),
    NO_COMMON_DIVISOR(7, 9, 1);

    final BigInteger a;
    final BigInteger b;
    final BigInteger expected;

    GcdTestCase(long a, long b, long expected) {
        this.a = BigInteger.valueOf(a);
        this.b = BigInteger.valueOf(b);
        this.expected = BigInteger.valueOf(expected);
    }
}

I created 11 test cases for the time being, but it's relatively easy to add test cases later. Just increase the enumerator.

Write a test method

Specify the ** @ParameterizedTest ** annotation for the test method, and at the same time specify the enum type created above using the ** @EnumSource ** annotation.

@ParameterizedTest
@EnumSource(GcdTestCase.class)
public void gcdShouldReturnGreatestCommonDivisorAsExpected(GcdTestCase test) {
    BigInteger actual = test.a.gcd(test.b);
    assertThat(actual).isEqualTo(test.expected);
}

Run the test

If you run the test you created with mvn test, of course, all the tests will succeed.

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running org.example.junit5.tips.BigIntegerTest
[INFO] Tests run: 11, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.156 s - in org.example.junit5.tips.BigIntegerTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 11, Failures: 0, Errors: 0, Skipped: 0

Summary

This time, I introduced how to easily write many test cases using ** @EnumSource ** of JUnit 5. The sample code is posted on GitHub. The license is The Unlicense.

Recommended Posts

JUnit 5: How to write test cases in enum
How to filter JUnit Test in Gradle
How to write Junit 5 organized
How to dynamically write iterative test cases using test / unit (Test :: Unit)
How to run JUnit in Eclipse
[Rails] How to write in Japanese
[RSpec] How to write test code
How to fix system date in JUnit
How to test private scope with JUnit
How to write an RSpec controller test
[SpringBoot] How to write a controller test
[Java] How to test for null with JUnit
[JavaFX] How to write Eclipse permissions in build.gradle
How to write Rails
How to test interrupts during Thread.sleep with JUnit
How to test private methods with arrays or variadic arguments in JUnit
How to use "sign_in" in integration test (RSpec)
How to write dockerfile
How to write docker-compose
How to write Mockito
How to use JUnit 5
How to write test code with Basic authentication
How to write migrationfile
How to write Java String # getBytes in Kotlin?
Notes on how to write comments in English
How to write a unit test for Spring Boot 2
How to write a date comparison search in Rails
How to test file upload screen in Spring + Selenium
How to write an external reference key in FactoryBot
How to write a core mod in Minecraft Forge 1.15.2
How to write good code
[Rails] How to use enum
How to use JUnit (beginner)
Bit Tetris (how to write)
[Rails] How to use enum
How to write java comments
Refer to enum in Thymeleaf
[Refactoring] How to write routing
Test private methods in JUnit
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
Test private methods in JUnit
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
How to migrate from JUnit4 to JUnit5
[Creating] How to use JUnit
How to use Java enums (Enum) in MyBatis Mapper XML
[Rails5] japanMap link How to write parameters in js.erb file
[Rails] How to write user_id (foreign key) in strong parameter
How to use Lombok in Spring
How to find May'n in XPath
How to hide scrollbars in WebView
How to iterate infinitely in Ruby
Studying Java # 6 (How to write blocks)
How to run Ant in Gradle
Write test code in Spring Boot
How to master programming in 3 months
How to learn JAVA in 7 days
How to get parameters in Spark