Home Migrating from JUnit 4 to JUnit 5
Post
Cancel

Migrating from JUnit 4 to JUnit 5

Migrating from JUnit 4 to JUnit 5

JUnit is a popular unit testing framework for Java applications that allows developers to write automated tests to verify that their code is working as expected. JUnit 5 is the latest version of JUnit and introduces several new features and improvements over JUnit 4. In this blog post, we will discuss the steps involved in migrating from JUnit 4 to JUnit 5.

Update Dependencies

The first step in migrating from JUnit 4 to JUnit 5 is to update the dependencies in your project. JUnit 5 consists of several modular jars, so you can pick and choose the jars that you need based on your testing needs. The most commonly used JUnit 5 dependencies are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!-- JUnit 5 Jupiter Engine -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.8.1</version>
    <scope>test</scope>
</dependency>

<!-- JUnit 5 Jupiter API -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.8.1</version>
    <scope>test</scope>
</dependency>

<!-- JUnit 5 Vintage Engine -->
<dependency>
    <groupId>org.junit.vintage</groupId>
    <artifactId>junit-vintage-engine</artifactId>
    <version>5.8.1</version>
    <scope>test</scope>
</dependency>

Replace Annotations

JUnit 5 has several new annotations that are not present in JUnit 4. Some of the new annotations include @BeforeEach, @AfterEach, @BeforeAll, @AfterAll, and @TestFactory. These annotations provide more flexibility and control over the test lifecycle. You can replace the JUnit 4 annotations with their JUnit 5 counterparts, as shown below:

JUnit 4 AnnotationJUnit 5 Annotation
@Before@BeforeEach
@After@AfterEach
@BeforeClass@BeforeAll
@AfterClass@AfterAll
@Tes@Test

Assertions

JUnit 5 has several new assertion methods that are not present in JUnit 4. The new assertions include assertions for arrays, strings, exceptions, and timeouts. You can replace the JUnit 4 assertions with their JUnit 5 counterparts, as shown below:

  • assertAll() : The assertAll() method allows you to group multiple assertions together and report all of them at once. This is useful when you want to ensure that multiple assertions pass or fail together, rather than stopping at the first failure.
1
2
3
4
5
6
7
8
9
10
@Test
void testPerson() {
    Person person = new Person("John", "Doe", 30);
    assertAll("person",
        () -> assertEquals("John", person.getFirstName()),
        () -> assertEquals("Doe", person.getLastName()),
        () -> assertEquals(30, person.getAge())
    );
}

In JUnit 4, runners were used to control the test execution flow. In JUnit 5, runners are replaced with extensions. You can use extensions to add functionality to your tests. For example, the @ExtendWith annotation can be used to add custom extensions to your tests.

  • assertTimeout() : The assertTimeout() method allows you to test whether a piece of code completes within a specified time limit. This is useful when you want to ensure that a method call or a block of code does not take too long to execute.
1
2
3
4
5
6
7
@Test
void testTimeout() {
    assertTimeout(Duration.ofSeconds(1), () -> {
        Thread.sleep(500);
    });
}

  • assertThrows() : The assertThrows() method allows you to test whether a specific exception is thrown by a piece of code. This is useful when you want to ensure that your code handles exceptions correctly.
1
2
3
4
5
6
7
8
@Test
void testException() {
    String str = null;
    assertThrows(NullPointerException.class, () -> {
        str.length();
    });
}

  • assertEquals() overloads : JUnit 5 provides several new overloads of the assertEquals() method. For example, you can now compare arrays and collections using the assertEquals() method.
1
2
3
4
5
6
7
@Test
void testArray() {
    String[] expected = {"foo", "bar", "baz"};
    String[] actual = {"foo", "bar", "baz"};
    assertArrayEquals(expected, actual);
}

  • assertLinesMatch() :
1
2
3
4
5
6
7
@Test
void testLines() {
    List<String> expected = Arrays.asList("foo", "bar", "baz");
    List<String> actual = Arrays.asList("  foo  ", "bar", "baz  ");
    assertLinesMatch(expected, actual);
}

Test Suites

In JUnit 4, test suites were created using the @SuiteClasses annotation. In JUnit 5, test suites can be created using the @SelectPackages and @SelectClasses annotations. The @SelectPackages annotation allows you to select a package and include all tests in that package, while the @SelectClasses annotation allows you to select specific test classes to include in the test suite.

Dynamic Tests

JUnit 5 introduces dynamic tests, which allows developers to generate tests at runtime. Dynamic tests can be created using the @TestFactory annotation, which returns a Stream, Collection, or Iterable of DynamicTest instances. Dynamic tests are useful when the number of tests or test inputs are not known until runtime.

Parameterized Tests

Parameterized tests in JUnit 5 are more flexible and powerful than in JUnit 4. In JUnit 5, parameterized tests can be written using the @ParameterizedTest annotation, which allows you to specify the source of the test parameters. The sources include CSV files, Excel files, JSON files, and methods that return a Stream, Collection, or Iterable of arguments.

Tagging and Filtering JUnit 5 has introduced tagging and filtering to make it easier to organize and run tests. Tags can be applied to individual test methods or entire test classes, and tests can be filtered based on tags. For example, you can tag tests with @Slow or @Fast, and then run only the slow tests using the –include-tag command line option.

Conclusion

Migrating from JUnit 4 to JUnit 5 is not a difficult process, but it does require some changes to your existing test code. By updating your dependencies, replacing annotations, and using new features such as dynamic tests and parameterized tests, you can take advantage of the new features and improvements in JUnit 5. Additionally, it’s important to note that JUnit 5 is not backwards compatible with JUnit 4, so it’s essential to test your code thoroughly after migrating to JUnit 5.

This post is licensed under CC BY 4.0 by the author.