Quick revision
The short answer
We add the junit-vintage-engine next to junit-jupiter, and the JUnit Platform then runs JUnit 4 tests through Vintage and JUnit 5 tests through Jupiter in the same build, so we can migrate one class at a time (Vintage is deprecated in newer releases, so check your version). Most annotations map directly, @Before to @BeforeEach, @Ignore to @Disabled, @Category to @Tag, but rules and runners have no one-to-one replacement: they become extensions, with partial help from junit-jupiter-migrationsupport, and @Test(expected) and @Test(timeout) become assertThrows and @Timeout. OpenRewrite recipes automate most of the mechanical change.
What to remember
In this answer, we saw that the JUnit Platform runs JUnit 4 tests through the Vintage engine and JUnit 5 tests through Jupiter in the same build, which allows a class-by-class migration, keeping in mind that Vintage is deprecated in newer releases. Lifecycle annotations, @Ignore and @Category map directly, @Test(expected) and @Test(timeout) become assertThrows and @Timeout, and rules and runners become extensions, with migrationsupport covering only a few rules. OpenRewrite's migration recipe automates most of the mechanical work.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We add the junit-vintage-engine next to junit-jupiter, and the JUnit Platform then runs JUnit 4 tests through Vintage and JUnit 5 tests through Jupiter in the same build, so we can migrate one class at a time (Vintage is deprecated in newer releases, so check your version). Most annotations map directly, @Before to @BeforeEach, @Ignore to @Disabled, @Category to @Tag, but rules and runners have no one-to-one replacement: they become extensions, with partial help from junit-jupiter-migrationsupport, and @Test(expected) and @Test(timeout) become assertThrows and @Timeout. OpenRewrite recipes automate most of the mechanical change.
Why this question is asked
Few teams can migrate thousands of JUnit 4 tests in one go, and they should not have to. JUnit 5 was designed so that both generations can run side by side while the migration happens gradually. Interviewers ask this because it combines architecture knowledge, how the platform runs two engines, with practical migration experience, knowing which changes are search-and-replace and which need thought. In this answer, we will set up the mixed project, go through the direct mappings, look closely at the features that do not map one to one, and see how automation handles the bulk of the work.
1Running Both Generations on One Platform
JUnit 5's JUnit Platform runs tests through engines. The Jupiter engine runs tests written with org.junit.jupiter.api. The Vintage engine runs tests written with JUnit 3 or 4, org.junit.Test. If both engines are on the test classpath, a single mvn test or gradle test runs both kinds, and the report lists them together.
- Maven, Gradle or the IDEone test run
- JUnit Platform Launcherasks every engine to discover tests
- Jupiter engine | Vintage engine
org.junit.jupiter.api.Test|org.junit.Test - InvoiceServiceTest | LegacyInvoiceTestmigrated class | not yet migrated
4 layers, from the top down. Step through them one at a time.
Notice that nothing in the test classes changes. Each class is picked up by the engine that recognises its annotations. This lets a team write all new tests in Jupiter immediately and migrate old classes whenever they are touched. The Vintage engine is deprecated in newer JUnit releases, which is a signal that the mixed state should be temporary rather than permanent; check your version for its status.
2The Direct Mappings
Most JUnit 4 features have a direct Jupiter equivalent, and the change is mechanical. The lifecycle annotations were renamed to say what they do. Assertions moved to org.junit.jupiter.api.Assertions, with one trap: in JUnit 4 the optional message is the first argument, in Jupiter it is the last.
Test classes and methods in Jupiter also no longer need to be public; package-private is enough and is the usual style.
3Expected Exceptions and Timeouts
Two JUnit 4 features lived inside the @Test annotation itself: expected and timeout. Jupiter's @Test has no attributes, so both move into the test body or onto their own annotation. The change is an improvement, because assertThrows pins the exception to one statement and returns it for further checks.
Note one difference for timeouts: JUnit 4's timeout ran the test in a separate thread, while Jupiter's @Timeout runs it on the same thread by default. Tests that relied on ThreadLocal state behave better after the migration, not worse, but it is worth knowing if something changes.
4Rules and Runners: No One-to-One Replacement
This is where migration needs thought. JUnit 4 had two extension mechanisms. A runner, chosen with @RunWith, replaced the entire way a class was run, and a class could have only one. A rule, declared with @Rule or @ClassRule, wrapped each test with extra behaviour. Jupiter replaces both with a single extension model: @ExtendWith and @RegisterExtension, and any number of extensions can be combined.
Popular runners and rules already have Jupiter extensions, so for them the change is a swap. Spring's SpringRunner becomes SpringExtension, which @SpringBootTest already includes. MockitoJUnitRunner becomes @ExtendWith(MockitoExtension.class). Parameterized becomes @ParameterizedTest with a source annotation. TemporaryFolder becomes @TempDir. ExpectedException becomes assertThrows.
For rules without an equivalent, the junit-jupiter-migrationsupport module offers a bridge: @EnableRuleMigrationSupport lets Jupiter run rules that extend ExternalResource or Verifier, and ExpectedException, unchanged. It is a stopgap with limited scope (check its status in your version). Custom runners and other custom rules have to be rewritten as extensions, using callbacks such as BeforeEachCallback, AfterEachCallback or ParameterResolver.
5Automating the Mechanical Part
Renaming annotations and moving assertion messages across thousands of files by hand is slow and error-prone. OpenRewrite, a refactoring tool that runs as a Maven or Gradle plugin, has a JUnit 4 to 5 migration recipe that changes imports and annotations, converts @Test(expected) to assertThrows, moves messages to the last position, replaces common runners and rules with extensions, and updates the build dependencies. It typically handles the large majority of changes; what is left, usually custom rules and runners, is reviewed and rewritten by hand. Running it module by module keeps each pull request reviewable.
Check the recipe name and coordinates against the current OpenRewrite documentation, since recipe catalogues are reorganised from time to time.
6Follow-up questions
Can one test class mix JUnit 4 and JUnit 5 annotations?
It should not. Each engine only recognises its own annotations, so a class with org.junit.Test methods and Jupiter lifecycle methods will run in confusing ways, often with lifecycle methods ignored. Migrate a class completely, or not at all.
Does Spring Boot still include the Vintage engine?
No. spring-boot-starter-test stopped including Vintage in Boot 2.4, so a Boot project with remaining JUnit 4 tests must add junit-vintage-engine explicitly, or those tests silently stop running.
7Conclusion
In this answer, we saw that the JUnit Platform runs JUnit 4 tests through the Vintage engine and JUnit 5 tests through Jupiter in the same build, which allows a class-by-class migration, keeping in mind that Vintage is deprecated in newer releases. Lifecycle annotations, @Ignore and @Category map directly, @Test(expected) and @Test(timeout) become assertThrows and @Timeout, and rules and runners become extensions, with migrationsupport covering only a few rules. OpenRewrite's migration recipe automates most of the mechanical work.
Open with
During a migration I add junit-vintage-engine next to junit-jupiter. The JUnit Platform then runs both: the Jupiter engine picks up classes using org.junit.jupiter.api, and Vintage picks up classes using org.junit.Test, in the same build and the same report.
Then, if they want more
That lets us write all new tests in Jupiter and migrate old classes gradually. Vintage is deprecated in newer releases, so I would treat this as temporary.
Most changes are direct: @Before and @After become @BeforeEach and @AfterEach, @BeforeClass and @AfterClass become @BeforeAll and @AfterAll, @Ignore becomes @Disabled, @Category becomes @Tag, and assertion messages move from the first argument to the last. @Test(expected) becomes assertThrows, which is better because it applies to one statement and returns the exception, and @Test(timeout) becomes @Timeout or assertTimeout.
The real work is rules and runners. JUnit 4 had both, and a class could have only one runner; Jupiter replaces them with extensions through @ExtendWith and @RegisterExtension. Common ones have direct replacements, SpringExtension, MockitoExtension, @ParameterizedTest, @TempDir, but custom rules and runners need rewriting as extensions. junit-jupiter-migrationsupport can run ExternalResource, Verifier and ExpectedException rules as a stopgap.
For the mechanical part I would use OpenRewrite's JUnit 4 to 5 recipe, module by module.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- JUnit 4, JUnit 5, JUnit 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How can JUnit 4 and JUnit 5 tests run in the same project during a migration, and which...": https://scrivio-interview-library.netlify.app/java/junit/0afae152fb/