gzhao9 opened a new issue, #15979:
URL: https://github.com/apache/druid/issues/15979

   ### Description
   Hello Druid Developers,
   I have identified a recurring pattern of code duplication across several 
test classes in the Apache Druid project. Specifically, the process of mocking 
`ColumnValueSelector` objects is repeated in multiple test cases, leading to 
unnecessary code bloat and complexity.
   
   ### Mock clone in Test Classes
   
   - `ArrayDoubleGroupByColumnSelectorStrategyTest`
   - `ArrayLongGroupByColumnSelectorStrategyTest`
   - `ArrayStringGroupByColumnSelectorStrategyTest`
   
   ### Example of Code Duplication
   
   Currently, our test classes contain repeated blocks of code for mocking 
`ColumnValueSelector` objects and repeated  9 times, as shown below:
   
   ```java
   ColumnValueSelector columnValueSelector = 
Mockito.mock(ColumnValueSelector.class);
   
Mockito.when(columnValueSelector.getObject()).thenReturn(ImmutableList.of(1.0, 
2.0));
   ```
   
   ### Proposed Solution
   
   Introduce a new utility class, `CreateMockColumnValueSelector`, which 
centralizes the creation of mocked `ColumnValueSelector` objects. This class 
will provide a static method to return a mocked `ColumnValueSelector` with 
predefined behavior, reducing the duplication and improving code 
maintainability.
   
   
   ### Suggested Implementation
   
   Below is a proposed implementation snippet for the 
`CreateMockColumnValueSelector` utility class:
   
   ```java
   public class CreateMockColumnValueSelector {
       public static <T> ColumnValueSelector get(Object returnValue) {
           ColumnValueSelector columnValueSelector = 
Mockito.mock(ColumnValueSelector.class);
           
Mockito.when(columnValueSelector.getObject()).thenReturn(returnValue);
           return columnValueSelector;
       }
   }
   ```
   We only need to call get() to reduce repetition
   
   ```java
   ColumnValueSelector columnValueSelector = 
CreateMockColumnValueSelector.get(new Object[]{"f", "a"});
   ```
   By doing this, code repetition can be reduced. Make the test code clearer
   I'd love to hear your thoughts on this proposal! Do you agree with any of 
these changes, or do you have other insights or preferences?
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to