Migrate AssertJ @Rule soft assertions to SoftAssertionsExtension - #1105
Merged
Conversation
`JUnitSoftAssertions` and `JUnitBDDSoftAssertions` only report collected failures from `TestRule#apply`, which JUnit Jupiter never calls. Left as is, the migrated tests silently pass no matter what they assert. Replace such `@Rule` fields with `@InjectSoftAssertions` fields of the corresponding `SoftAssertions`/`BDDSoftAssertions` type, dropping `final` and the initializer as the extension requires, and register `@ExtendWith(SoftAssertionsExtension.class)` on the declaring class. Fixes #1097
The compilation unit wide `ChangeType` retyped every reference, including fields this recipe deliberately leaves alone: a `@ClassRule`, or a field only reachable through a `RuleChain`. Those still have to implement `TestRule`, so converting them broke compilation. Scan the compilation unit up front for such fields and skip their type entirely. Skipping is per type, so a `JUnitBDDSoftAssertions` rule still migrates alongside a blocked `JUnitSoftAssertions` `@ClassRule`. Also reuse the `ListUtils.map` plus `maybeAutoFormat` idiom from `TempDirNonFinal` to drop the `final` modifier.
On a Kotlin property with an explicit type the recipe threw `IllegalStateException: Expected to find a matching parent`, as the compilation unit wide `ChangeType` is driven from a `J.CompilationUnit` that a Kotlin source does not have. It only escaped notice because an inferred property type leaves `J.VariableDeclarations#getType()` null. Proper Kotlin support needs `lateinit var` rather than a dropped `final` and initializer, so skip Kotlin sources for now.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JUnitSoftAssertionsandJUnitBDDSoftAssertionsonly report their collected failures fromTestRule#apply, which JUnit Jupiter never calls.JUnit4to5Migrationleft those@Rulefields untouched, so where JUnit 4 survives on the classpath (transitively, or deliberately for Testcontainers) the migrated tests compile, go green, and assert nothing.HandleExternalResourceRulesdoesn't cover them: both types implementSoftAssertionsRule, notExternalResource.This adds
JUnitSoftAssertionsToSoftAssertionsExtension, wired intoJUnit4to5Migrationahead of theExternalResourcecatch-all. For an instance@Rulefield of either type it:@Rulefor@InjectSoftAssertions,SoftAssertions/BDDSoftAssertions,finaland the initializer, whichSoftAssertionsExtensionrequires (it rejects static or final fields and instantiates the provider itself),@ExtendWith(SoftAssertionsExtension.class)to the declaring class unless already present.becomes
@ClassRule/static fields are left alone, as the extension can't inject those.@Rule JUnitSoftAssertionsbehind, silently disabling the assertions #1097