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.
References: Junit5 User Guide - https://junit.org/junit5/docs/current/user-guide/#overview
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.
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:
TestRule (TestWatcher
and ExternalResource
) to Extension (TestWatcher
and AfterAllCallback
) example:
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):
Migration with calling addToTestExecutionObserver()
method example:
Migration with BasePageAutoRegistration
class example:
Test suites
Test suite migration example:
Running tests from Maven:
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:
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:
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:
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.setUp()
and BaseTest.tearDown()
methods are now not abstract and need no implementation in subclasses. @Override
when a custom implemenatation is needed.