You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remove unnecessary public modifier from JUnit 5 @BeforeEach setup method #8
SonarQube reported a maintainability issue in the file where the setup method annotated with @beforeeach is declared as public.
In JUnit 5, lifecycle methods such as @beforeeach, @AfterEach, and @test do not need to be public. These methods can use package-private visibility because the JUnit Jupiter engine invokes them through reflection. Declaring them as public unnecessarily exposes internal test implementation details and reduces encapsulation.
To follow JUnit 5 best practices and improve code maintainability, the public modifier should be removed from the setup method.
Current Code
@beforeeach
public void setup(TestUtils setup)
{
setup.loginAsSuperAdmin();
}
This issue has been resolved by removing the unnecessary public modifier from the @beforeeach lifecycle method in the affected test class, in accordance with JUnit 5 best practices and the recommendation from SonarQube.
In JUnit 5, lifecycle methods such as @beforeeach, @AfterEach, and @test do not need to be declared as public. The JUnit Jupiter engine invokes these methods through reflection, so package-private visibility is sufficient. Keeping the method public unnecessarily exposes internal test implementation details and goes against the principle of minimal visibility.
The method has been updated to remove the public modifier:
This change improves code encapsulation and aligns the test class with JUnit 5 conventions and SonarQube maintainability guidelines, while preserving the existing functionality of the test.
The fix resolves the technical debt / code smell reported by SonarQube.
The change has been implemented in the corresponding Pull Request #17.
Activity
irtaza780 commentedon Mar 9, 2026
This issue has been resolved by removing the unnecessary public modifier from the @beforeeach lifecycle method in the affected test class, in accordance with JUnit 5 best practices and the recommendation from SonarQube.
Previously, the setup method was declared as:
@beforeeach
public void setup(TestUtils setup)
In JUnit 5, lifecycle methods such as @beforeeach, @AfterEach, and @test do not need to be declared as public. The JUnit Jupiter engine invokes these methods through reflection, so package-private visibility is sufficient. Keeping the method public unnecessarily exposes internal test implementation details and goes against the principle of minimal visibility.
The method has been updated to remove the public modifier:
@beforeeach
void setup(TestUtils setup)
This change improves code encapsulation and aligns the test class with JUnit 5 conventions and SonarQube maintainability guidelines, while preserving the existing functionality of the test.
The fix resolves the technical debt / code smell reported by SonarQube.
The change has been implemented in the corresponding Pull Request #17.