Repository navigation
Call super from JUnit 3 setUp and tearDown overrides - #1257
Merged
Merged
Conversation
Sonar S2188: an override that does not call the inherited implementation silently skips the shared setup or cleanup an ancestor performs. `super.setUp()` is inserted first and `super.tearDown()` last. An override declaring no `throws` gains the clause the inherited method declares, which an override may always repeat, and without which the inserted call would not compile. An override whose `throws` is narrower is left alone, since widening it breaks a subclass that narrows it again and calls `super`. The bail-out check matches `super` by name rather than with a `MethodMatcher`, because the inserted call has no method type yet and a typed matcher would miss it and add a second one on the next cycle.
steve-aom-elliott
approved these changes
Nov 2, 2026
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.
Sonar rule S2188 reports JUnit 3
setUpandtearDownoverrides that do not call the inherited implementation, skipping the setup or cleanup an ancestor performs, and no recipe covered it yet. The newAddMissingSuperCallInTestCaserecipe addssuper.setUp()first andsuper.tearDown()last, so an ancestor initializes first and cleans up last.An override whose
throwsis missing or narrower than the inherited one gains the inherited clause, so the call compiles. It is aScanningRecipebecause a wider clause breaks code relying on the old signature, so the override is left alone when the repository extends its class, calls it, or has it implement an interface method.Only Java sources are changed. Also left alone: a class extending
TestCasedirectly, whose implementations are empty, an empty override, which turns the ancestor's off on purpose, anabstractancestor declaration, and atearDownnot ending in a simple statement, where the appended call could be unreachable. Asupercall inside a nested class or asFixture.super.setUp()does not count.The call survives
MigrateJUnitTestCase, which only strips calls toTestCase's own methods, and JUnit 5 still needs it, as an overriding@BeforeEachhides the ancestor's.Over the 71 Eclipse Foundation repositories with an LST it changed one file,
glassfish-grizzly'sByteBufferStreamsTest, which skipped the thread-cache tracking itsGrizzlyTestCaseparent sets.It is not in
cleanup.BestPractices, since restoring an ancestor's setup can turn a passing test red.Not covered: an unresolved parent method, callers in another repository, and a
tearDownending intryorif/else.