AMLSim
/
jars
/junit5-r5.10.2
/documentation
/src
/docs
/asciidoc
/user-guide
/migration-from-junit4.adoc
== Migrating from JUnit 4 | |
Although the JUnit Jupiter programming model and extension model do not support JUnit 4 | |
features such as `Rules` and `Runners` natively, it is not expected that source code | |
maintainers will need to update all of their existing tests, test extensions, and custom | |
build test infrastructure to migrate to JUnit Jupiter. | |
Instead, JUnit provides a gentle migration path via a _JUnit Vintage test engine_ which | |
allows existing tests based on JUnit 3 and JUnit 4 to be executed using the JUnit Platform | |
infrastructure. Since all classes and annotations specific to JUnit Jupiter reside under | |
the `org.junit.jupiter` base package, having both JUnit 4 and JUnit Jupiter in the | |
classpath does not lead to any conflicts. It is therefore safe to maintain existing JUnit | |
4 tests alongside JUnit Jupiter tests. Furthermore, since the JUnit team will continue to | |
provide maintenance and bug fix releases for the JUnit 4.x baseline, developers have | |
plenty of time to migrate to JUnit Jupiter on their own schedule. | |
=== Running JUnit 4 Tests on the JUnit Platform | |
Make sure that the `junit-vintage-engine` artifact is in your test runtime path. In that | |
case JUnit 3 and JUnit 4 tests will automatically be picked up by the JUnit Platform | |
launcher. | |
See the example projects in the {junit5-samples-repo}[`junit5-samples`] repository to | |
find out how this is done with Gradle and Maven. | |
==== Categories Support | |
For test classes or methods that are annotated with `@Category`, the _JUnit Vintage test | |
engine_ exposes the category's fully qualified class name as a <<running-tests-tags, tag>> | |
for the corresponding test class or test method. For example, if a test method is | |
annotated with `@Category(Example.class)`, it will be tagged with `"com.acme.Example"`. | |
Similar to the `Categories` runner in JUnit 4, this information can be used to filter the | |
discovered tests before executing them (see <<running-tests>> for details). | |
=== Migration Tips | |
The following are topics that you should be aware of when migrating existing JUnit 4 | |
tests to JUnit Jupiter. | |
* Annotations reside in the `org.junit.jupiter.api` package. | |
* Assertions reside in `org.junit.jupiter.api.Assertions`. | |
- Note that you may continue to use assertion methods from `org.junit.Assert` or any | |
other assertion library such as {AssertJ}, {Hamcrest}, {Truth}, etc. | |
* Assumptions reside in `org.junit.jupiter.api.Assumptions`. | |
- Note that JUnit Jupiter 5.4 and later versions support methods from JUnit 4's | |
`org.junit.Assume` class for assumptions. Specifically, JUnit Jupiter supports JUnit | |
4's `AssumptionViolatedException` to signal that a test should be aborted instead of | |
marked as a failure. | |
* `@Before` and `@After` no longer exist; use `@BeforeEach` and `@AfterEach` instead. | |
* `@BeforeClass` and `@AfterClass` no longer exist; use `@BeforeAll` and `@AfterAll` | |
instead. | |
* `@Ignore` no longer exists: use `@Disabled` or one of the other built-in | |
<<writing-tests-conditional-execution, execution conditions>> instead | |
- See also <<migrating-from-junit4-ignore-annotation-support>>. | |
* `@Category` no longer exists; use `@Tag` instead. | |
* `@RunWith` no longer exists; superseded by `@ExtendWith`. | |
* `@Rule` and `@ClassRule` no longer exist; superseded by `@ExtendWith` and | |
`@RegisterExtension`. | |
- See also <<migrating-from-junit4-rule-support>>. | |
* `@Test(expected = ...)` and the `ExpectedException` rule no longer exist; use | |
`Assertions.assertThrows(...)` instead. | |
- See <<migrating-from-junit4-rule-support>> if you still need to use | |
`ExpectedException`. | |
* Assertions and assumptions in JUnit Jupiter accept the failure message as their last | |
argument instead of the first one. | |
- See <<migrating-from-junit4-failure-message-arguments>> for details. | |
=== Limited JUnit 4 Rule Support | |
As stated above, JUnit Jupiter does not and will not support JUnit 4 rules natively. The | |
JUnit team realizes, however, that many organizations, especially large ones, are likely | |
to have large JUnit 4 code bases that make use of custom rules. To serve these | |
organizations and enable a gradual migration path the JUnit team has decided to support a | |
selection of JUnit 4 rules verbatim within JUnit Jupiter. This support is based on | |
adapters and is limited to those rules that are semantically compatible to the JUnit | |
Jupiter extension model, i.e. those that do not completely change the overall execution | |
flow of the test. | |
The `junit-jupiter-migrationsupport` module from JUnit Jupiter currently supports the | |
following three `Rule` types including subclasses of these types: | |
* `org.junit.rules.ExternalResource` (including `org.junit.rules.TemporaryFolder`) | |
* `org.junit.rules.Verifier` (including `org.junit.rules.ErrorCollector`) | |
* `org.junit.rules.ExpectedException` | |
As in JUnit 4, Rule-annotated fields as well as methods are supported. By using these | |
class-level extensions on a test class such `Rule` implementations in legacy code bases | |
can be _left unchanged_ including the JUnit 4 rule import statements. | |
This limited form of `Rule` support can be switched on by the class-level annotation | |
`{EnableRuleMigrationSupport}`. This annotation is a _composed annotation_ which enables | |
all rule migration support extensions: `VerifierSupport`, `ExternalResourceSupport`, and | |
`ExpectedExceptionSupport`. You may alternatively choose to annotate your test class with | |
`@EnableJUnit4MigrationSupport` which registers migration support for rules _and_ JUnit | |
4's `@Ignore` annotation (see <<migrating-from-junit4-ignore-annotation-support>>). | |
However, if you intend to develop a new extension for JUnit Jupiter please use the new | |
extension model of JUnit Jupiter instead of the rule-based model of JUnit 4. | |
=== JUnit 4 @Ignore Support | |
In order to provide a smooth migration path from JUnit 4 to JUnit Jupiter, the | |
`junit-jupiter-migrationsupport` module provides support for JUnit 4's `@Ignore` | |
annotation analogous to Jupiter's `{Disabled}` annotation. | |
To use `@Ignore` with JUnit Jupiter based tests, configure a _test_ dependency on the | |
`junit-jupiter-migrationsupport` module in your build and then annotate your test class | |
with `@ExtendWith(IgnoreCondition.class)` or `{EnableJUnit4MigrationSupport}` (which | |
automatically registers the `IgnoreCondition` along with | |
<<migrating-from-junit4-rule-support>>). The `IgnoreCondition` is an | |
`{ExecutionCondition}` that disables test classes or test methods that are annotated with | |
`@Ignore`. | |
---- | |
include::{testDir}/example/IgnoredTestsDemo.java[tags=user_guide] | |
---- | |
=== Failure Message Arguments | |
The `Assumptions` and `Assertions` classes in JUnit Jupiter declare arguments in a | |
different order than in JUnit 4. In JUnit 4 assertion and assumption methods accept the | |
failure message as the first argument; whereas, in JUnit Jupiter assertion and assumption | |
methods accept the failure message as the last argument. | |
For instance, the method `assertEquals` in JUnit 4 is declared as `assertEquals(String | |
message, Object expected, Object actual)`, but in JUnit Jupiter it is declared as | |
`assertEquals(Object expected, Object actual, String message)`. The rationale for this is | |
that a failure message is _optional_, and optional arguments should be declared after | |
required arguments in a method signature. | |
The methods affected by this change are the following: | |
- Assertions | |
* `assertTrue` | |
* `assertFalse` | |
* `assertNull` | |
* `assertNotNull` | |
* `assertEquals` | |
* `assertNotEquals` | |
* `assertArrayEquals` | |
* `assertSame` | |
* `assertNotSame` | |
* `assertThrows` | |
- Assumptions | |
* `assumeTrue` | |
* `assumeFalse` | |