Migration from JUnit4 to JUnit5

Migration guide

Junit4 to Junit5 migration guide

mrchecker-core-module version 5.6.2.1 features the upgrade of Junit4 to Junit5. Consequently, the Junit4 features are now obsolete and current test projects require migration in order to use the latest revision of MrChecker. This site provides guidance on the migration.

POM

The project pom.xml file needs to be adjusted in the first place. An exemplary POM file for download can be found here: https://github.com/devonfw/mrchecker/blob/develop/template/pom.xml

Test Annotations

Junit5 redefines annotations defining a test flow. The annotations need to be adjusted as per the following table.

migration01
Rule, ClassRule, TestRule and TestMethod

Junit4 @Rule and @ClassRule annoations as well as TestRule and TestMethod interfaces have been replaced with the Junit5 extension mechanism (https://junit.org/junit5/docs/current/user-guide/#extensions). During the migration to Junit5, all the instances of the mentioned types need to be rewritten according to the Junit5 User Guide. The extension mechanism is far more flexible than the Junit4 functionality based on rules.

Note: as per Junit5 API spec: ExpectedExceptionSupport, ExternalResourceSupport, VerifierSupport provide native support of the correspoinding Junit4 rules.

Extension registration example:

migration02
migration arrow down
migration03

TestRule (TestWatcher and ExternalResource) to Extension (TestWatcher and AfterAllCallback) example:

migration04
migration arrow down
migration05
Page, BasePageAutoRegistration and PageFactory classes

Page class is a new MrChecker class. It was introduced to provide common implemenation for its subpages in specific MrChecker modules. In order to receive test lifecycle notifications, particular Pages need to be registered by calling addToTestExecutionObserver() method. To facilitate this process, PageFactory class was designed and it’s usage is a recommended way of creating Page objects for tests. Although in MrChecker based on Junit4, the registration process was done in a specific BasePage constructor, it’s been considered error prone and reimplemented. Furthermore, to reduce migration cost BasePageAutoRegistration classes are available in MrChceker modules. They use the old way of registration. Given that three ways of migration are possible.

Migration with PageFactory class example (RECOMMENDED):

migration06
migration arrow down
migration07

Migration with calling addToTestExecutionObserver() method example:

migration06
migration arrow down
migration08

Migration with BasePageAutoRegistration class example:

migration09
migration arrow down
migration10
Test suites

Test suite migration example:

migration11
migration arrow down
migration12

Running tests from Maven:

migration13
migration arrow down
migration14
Concurrency

Junit5 provides native thread count and parallel execution control in contrast to Junit4 where it was controlled by Maven Surefire plugin. To enable concurrent test execution, junit-platform.properties file needs to placed in the test/resources directory of a project.

Exemplary file contents:

migration15

A ready-to-use file can be found here.

MrChecker supports only concurrent test class execution. @ResourceLock can be used to synchronize between classes if needed:

migration16
Cucumber

If Cucumber is used in a project, it is neccessary to change a hook class. An exemplary hook source file for download can be found here.

Data driven tests

Junit5 implements new approach to data driven tests by various data resolution mechanisms.

An example of method source parameters migration version one:

migration17
migration arrow down
migration18

An example of method source parameters migration version two:

migration17
migration arrow down
migration19

An example of method source in another class parameters migration:

migration20
migration arrow down
migration21

Providing parameters directly in annotations has no analogy in Junit5 and needs to be replaced with e.g. method source:

migration22
migration arrow down
migration23

An example of csv parameters source with no header line migration:

migration24
migration arrow down
migration25

An example of csv parameters source with the header line migration:

migration26
migration arrow down
migration27

An example of csv parameters source with object mapping migration step1:

migration28
migration arrow down
migration29

An example of csv parameters source with object mapping migration step 2:

migration30
migration arrow down
migration31
setUp() and tearDown()

BaseTest.setUp() and BaseTest.tearDown() methods are now not abstract and need no implementation in subclasses. @Override when a custom implemenatation is needed.

Last updated 2023-11-20 10:37:01 UTC