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
While working with the ServletPipelineRequestDispatcherTest, I've noticed that there are four mock variables repeatedly created across various tests. To simplify the code, I propose a small refactor to eliminate these redundancies, which could reduce the code by 60 lines.
Here's a summary of the repetitive mock creations:
Binding<HttpServlet>: Repeated mocked 3 times
Binding<ServletDefinition>: Repeated mocked 3 times
Injector: Repeated mocked 3 times
HttpServletRequest: Repeated mocked 4 times
For instance, creating a mock for HttpServletRequest currently looks like this:
Similarly, for the mock creation of Binding<HttpServlet>, Binding<ServletDefinition> and Injector We can introduce methods for creating mocks for these classes as well:
Using these methods, the refactored code becomes much cleaner:
finalBinding<HttpServlet> binding = createMockBinding();
// Other code in the test caseBinding<ServletDefinition> mockBinding = createMockBinding(servletDefinition);
finalInjectorinjector = createMockInjector(binding, mockServlet, mockBinding);
And for further improvement, we can overload createMockInjector for a more streamlined approach:
Hi there!
While working with the
ServletPipelineRequestDispatcherTest
, I've noticed that there are four mock variables repeatedly created across various tests. To simplify the code, I propose a small refactor to eliminate these redundancies, which could reduce the code by 60 lines.Here's a summary of the repetitive mock creations:
Binding<HttpServlet>
: Repeated mocked 3 timesBinding<ServletDefinition>
: Repeated mocked 3 timesInjector
: Repeated mocked 3 timesHttpServletRequest
: Repeated mocked 4 timesFor instance, creating a mock for
HttpServletRequest
currently looks like this:To make this process more efficient, we can introduce a
createMockRequest
method:With this method, creating a mock
HttpServletRequest
becomes:Similarly, for the mock creation of
Binding<HttpServlet>
,Binding<ServletDefinition>
andInjector
We can introduce methods for creating mocks for these classes as well:mock
Binding<HttpServlet>
creation:mock
Binding<ServletDefinition>
creation:mock
Injector
creation:The code to create the mock before using these methods looks like this:
Using these methods, the refactored code becomes much cleaner:
And for further improvement, we can overload
createMockInjector
for a more streamlined approach:This final refactored code is:
I’ve created a draft PR in my forked project where you can see the detailed changes here.
The refactor reduced the test cases by 60 lines of code, and I believe these changes will improve code readability.
The text was updated successfully, but these errors were encountered: