Repository navigation
Description
JUnit4to5Migration converts each test file on its own and then removes JUnit 4 from the build unconditionally: RemoveDependency junit:junit, RemoveDependency and ExcludeDependency of junit-vintage-engine. A test class that uses something the migration cannot convert, such as a custom runner or a custom rule, ends up half migrated. Either it no longer compiles, because JUnit 4 is gone, or its tests silently stop running: the class keeps its JUnit 4 @RunWith or @Rule, but its @Test methods are now Jupiter's, and nothing runs them.
Where this was found
A Moderne run of org.openrewrite.java.testing.junit5.JUnit4to5Migration over the 14 Dropwizard repositories (run 20261003120604-3Dm1q), reviewed by a team of coding agents that recorded each problem in the run's io.moderne.RecipeIssues data table. Four issue ids there are this one problem:
| Issue id | Rows | Repositories | Generalized example |
|---|---|---|---|
unsupported-rule-lifecycle |
5 | dropwizard/dropwizard-testing-junit4 | @Rule CustomRule rule (a TestRule) stays, @org.junit.Test becomes @org.junit.jupiter.api.Test: the rule never runs |
unsupported-runner-lifecycle |
1 | dropwizard/metrics | @RunWith(CustomRunner.class) (a BlockJUnit4ClassRunner that installs a class loader) stays, @Test becomes Jupiter's: the runner is bypassed |
classrule-external-resource-ignored |
6 | dropwizard/dropwizard-testing-junit4, dropwizard/metrics | HandleExternalResourceRules adds @ExtendWith(ExternalResourceSupport.class) for a @ClassRule ExternalResource, but ExternalResourceSupport only executes @Rule members, so before()/after() never run |
junit4-dependency-still-used |
3 | dropwizard/dropwizard-testing-junit4, dropwizard/metrics | production code implements org.junit.rules.TestRule, and the compile-scoped junit:junit dependency is removed anyway |
What was tried and closed
#1162-#1165 gated the whole migration per module: a new FindJUnit4MigrationCandidates precondition skipped any module whose @RunWith runner or @Rule type was not on a fixed list of types the migration knows how to convert, or whose production code uses JUnit 4. The test suite passed, but the list cannot know about conversions that live outside this module, so it would have stopped modules from migrating that migrate fine today:
SpringRunnerandSpringJUnit4ClassRunner, which rewrite-spring'sSpringBoot2JUnit4to5Migrationconverts while including this recipe. There it would be worse than today: rewrite-spring removes@RunWith, but the tests stay on JUnit 4.PowerMockRunner, whichReplacePowerMockitoreplaces inside this migration.- Any
ExternalResourcesubclass, whichHandleExternalResourceRulesadapts (the list matched rule types exactly). - System Rules, if Migrate System Rules to System Stubs and JUnit Pioneer #1152 lands.
Proposal
Convert what can be converted, and leave a class that cannot be finished entirely on JUnit 4, still running.
- Specific converters first, then generic converters guarded per class. Order the composite so that the recipes that convert runners and rules (
ParameterizedRunnerToParameterized,JUnitParamsRunnerToParameterized,UseMockitoExtension,EnclosedToNested,VertxUnitToVertxJunit5,TemporaryFolderToTempDir,ExpectedExceptionToAssertThrows,HandleExternalResourceRules, the Arquillian, Byteman and PowerMock recipes, ...) run before the generic ones (UpdateTestAnnotation,UpdateBeforeAfterAnnotations,AddMissingTestBeforeAfterAnnotations,AssertToAssertions,MigrateAssumptions, ...). Give each generic converter a precondition that skips a class that still carries a JUnit 4@RunWith,@Ruleor@ClassRule. The check runs when the class is visited, not as a scan beforehand, and recipes run in cycles, so a class whose runner a wrapping composite converts later (rewrite-spring'sSpringBoot2JUnit4to5Migration) is finished on the next cycle. No list of supported runners or rules is needed anywhere. - Keep JUnit 4 where it is still used. Remove
junit:junitonly when no JUnit 4 type is left in the module (RemoveDependencywithunlessUsingonorg.junit.*,org.junit.rules..*,org.junit.runner..*,org.junit.runners..*,junit.framework..*;org.junit.*does not matchorg.junit.jupiter). In such a module, keepjunit-vintage-engine(or add it) instead of removing and excluding it, so the classes left on JUnit 4 keep running on the JUnit Platform. This also covers production code that uses the JUnit 4 API, with no special case. HandleExternalResourceRulesshould not claim@ClassRulefields until there is a class-level counterpart toExternalResourceSupport. With (1), such a class then stays on JUnit 4 and keeps working.
Tests this needs
- A custom runner and a custom
TestRule: the class is left untouched,junit:junitstays,junit-vintage-engineis present. - A
@ClassRuleExternalResource: same. - Production code implementing
TestRule:junit:junitstays in compile scope. - A class whose runner is converted by a recipe that runs after the migration (simulating rewrite-spring's composite): fully migrated after the next cycle.
- The existing PowerMock and Mockito runner paths: unchanged.
- rewrite-spring's
SpringBoot2JUnit4to5MigrationTestagainst this change.
Open questions
- Is keeping or adding
junit-vintage-enginein partly migrated modules acceptable as the default, or should it be an option, with the current "remove JUnit 4 everywhere" behaviour as the alternative? - Class or file granularity for the guard: a file with two test classes, one convertible and one not.
- How this interacts with
ExcludeJUnit4UnlessUsingTestcontainers.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity