Skip to content

Remove unnecessary public modifier from JUnit 5 @BeforeEach setup method #8

Closed

Description

@irtaza780

Issue Description

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();
}

Recommended Change

@beforeeach
void setup(TestUtils setup)
{
setup.loginAsSuperAdmin();
}

This change does not affect functionality but aligns the code with JUnit 5 conventions and SonarQube rule recommendations.

Type: Technical Debt
Category: Maintainability / Code Smell

Activity

  1. self-assigned this
    on Mar 9, 2026
  2. irtaza780 commented on Mar 9, 2026

    @irtaza780
    CollaboratorAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions