Skip to content

JUnit4to5Migration: leave a class it cannot finish on JUnit 4, still running, instead of half-migrating it #1167

Open

Description

@jkschneider

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:

  • SpringRunner and SpringJUnit4ClassRunner, which rewrite-spring's SpringBoot2JUnit4to5Migration converts while including this recipe. There it would be worse than today: rewrite-spring removes @RunWith, but the tests stay on JUnit 4.
  • PowerMockRunner, which ReplacePowerMockito replaces inside this migration.
  • Any ExternalResource subclass, which HandleExternalResourceRules adapts (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.

  1. 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, @Rule or @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's SpringBoot2JUnit4to5Migration) is finished on the next cycle. No list of supported runners or rules is needed anywhere.
  2. Keep JUnit 4 where it is still used. Remove junit:junit only when no JUnit 4 type is left in the module (RemoveDependency with unlessUsing on org.junit.*, org.junit.rules..*, org.junit.runner..*, org.junit.runners..*, junit.framework..*; org.junit.* does not match org.junit.jupiter). In such a module, keep junit-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.
  3. HandleExternalResourceRules should not claim @ClassRule fields until there is a class-level counterpart to ExternalResourceSupport. 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:junit stays, junit-vintage-engine is present.
  • A @ClassRule ExternalResource: same.
  • Production code implementing TestRule: junit:junit stays 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 SpringBoot2JUnit4to5MigrationTest against this change.

Open questions

  • Is keeping or adding junit-vintage-engine in 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions