averbraeck / opentrafficsim

Open Source Multi-Level Traffic Simulator
BSD 3-Clause "New" or "Revised" License
28 stars 8 forks source link

Update unit tests from JUnit 4 to JUnit 5 #84

Closed averbraeck closed 12 months ago

averbraeck commented 12 months ago

JUnit 5 has been the new standard for unit tests for quite a while now (since 2017). The big challenge for updating from version 4 to version 5 is that the order of the parameters in the various static assert statements such as assertEquals or assertNull has changed. Optional arguments such as the String message were at the front, where optional arguments are typically at the back. JUnit 5 puts these optional arguments at the back, leading to a massive number of changes in the unit test code. The package structure has also changed considerably.

JUnit 5 can be included as:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>
averbraeck commented 12 months ago

This upgrade is done in a separate branch.

averbraeck commented 12 months ago

GTUDumperTest uses a @Rule with TemporaryFolder that have not been ported in a one-to-one way to JUnit 5. See See https://stackoverflow.com/questions/56613895/how-to-get-rid-of-temporaryfolder-rule-in-junit5 and https://www.baeldung.com/junit-5-temporary-directory for solutions.

The following code:

    @Rule
    public TemporaryFolder testDir = new TemporaryFolder();

can be replaced with:

    @TempDir
    private Path testDir;

and a statement like:

    this.containerDir = this.testDir.newFolder("subfolder");

can be replaced with

    Path containerPath = Files.createDirectory(Paths.get(this.testDir.toString() + File.separator + "subfolder"));
    this.containerDir = containerPath.toFile();
averbraeck commented 12 months ago

Upgrade executed. All unit tests pass again. Branch will be integrated into main branch.