Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Test Code for Improved Readability and Reduct LOC #14767

Closed
wants to merge 3 commits into from

Conversation

gzhao9
Copy link

@gzhao9 gzhao9 commented Mar 17, 2024

Code Refactoring Summary

In the process of improving code quality and readability within our project, a significant redundancy was identified and addressed. This documentation outlines the changes made, particularly focusing on the optimization of test code for enhanced maintainability and clarity.

Background

Upon review, it was found that the creation of mock instances of SecurityContextHolderStrategy was consistently replicated across multiple test classes. These classes are as follows:

  • AuthorizationManagerAfterMethodInterceptorTests
  • AuthorizationManagerBeforeMethodInterceptorTests
  • PreFilterAuthorizationMethodInterceptorTests
  • PostFilterAuthorizationMethodInterceptorTests

The mock creation process was identical in each instance, involving the setup of a SecurityContextImpl as the return value for getContext(). The repeated code snippet across eight test cases is shown below:

SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
// other statements
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));

This repetition not only decreased code readability due to the interspersion of other statements but also led to unnecessary duplication.

Solution

To eliminate this redundancy and improve code readability, a utility class named MockSecurityContextHolderStrategy was created. This class contains a static method getmock that encapsulates the mock creation logic, thereby enabling reuse across the affected test cases. The implementation of the getmock method is as follows:

public class MockSecurityContextHolderStrategy {
    static SecurityContextHolderStrategy getmock(SecurityContextImpl securityContextImpl){

        SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
        given(strategy.getContext()).willReturn(securityContextImpl);
        return strategy;
    }
}

Changes Made

  • Before Refactoring:

Each of the eight test cases across the four specified test classes directly created a mock of SecurityContextHolderStrategy, interspersed with other statements, which cluttered the test code.

  • After Refactoring:

The direct creation of mocks has been replaced with calls to MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication)), significantly simplifying the test code and avoiding duplication.

SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));

Impact

It exemplifies a step towards adhering to DRY (Don't Repeat Yourself) principles, making our codebase more efficient and easier to manage.

@pivotal-cla
Copy link

@gzhao9 Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 17, 2024
@gzhao9 gzhao9 closed this Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants