Migration from JUnit4 to JUnit5
Junit4 to Junit5 migration guide
mrchecker-core-module version 18.104.22.168 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.
References: Junit5 User Guide - https://junit.org/junit5/docs/current/user-guide/#overview
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
Junit5 redefines annotations defining a test flow. The annotations need to be adjusted as per the following table.
Rule, ClassRule, TestRule and TestMethod
@ClassRule annoations as well as
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:
ExternalResource) to Extension (
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
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.
PageFactory class example (RECOMMENDED):
Migration with calling
addToTestExecutionObserver() method example:
BasePageAutoRegistration class example:
Test suite migration example:
Running tests from Maven:
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:
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:
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:
An example of method source parameters migration version two:
An example of method source in another class parameters migration:
Providing parameters directly in annotations has no analogy in Junit5 and needs to be replaced with e.g. method source:
An example of csv parameters source with no header line migration:
An example of csv parameters source with the header line migration:
An example of csv parameters source with object mapping migration step1:
An example of csv parameters source with object mapping migration step 2:
setUp() and tearDown()
BaseTest.tearDown() methods are now not abstract and need no implementation in subclasses.
@Override when a custom implemenatation is needed.