I read this question: How do I test a class that has private methods, fields or inner classes? and it seems that I might have a code smell, but my code is very simple to actually refactor. what is wrong in the design that I have created.
I have created a delegate class for processing some actions it has three methods execute(Action);
PopulateActionHandlers()
and executeActionhandlers();
My class is like below:
public class DelegateHandler{
Map<Integer,ActionHandlers> handlerMaps;
public execute(Action action){
populateActionHandlers(action);
executeActionHandlers();
}//end of execute
//This method can create and populate more than one handlers in handlerMap
private populateActionHandlers(action){
handlerMap = new LinkedHashMap<ActionHandlers>();
if (action.isMultimode()){
handlerMap.add(1,new handler(action.getabc()));
handlerMap.add(2,new handler(action.getabc()-1));
}else{
handlerMap.add(1,new handler(action));
}
}//end of populateActionHandlers
//This method can execute more than one handlers in handlerMap
private executeActionHandlers(){
for(ActionHandler actionHandler : handlerMap.values){
actionHandler.executeAction();
}
}//end of executeActionHandlers
}
Now I want to test populateActionHandlers()
method with JUnit, which I made private as there is no need to expose it outside this class. If I test the execute()
method then it will test both populateActionHandlers()
and executeActionHandlers()
methods which is testing two units at the same time, I want to test them separately. The design (I think) seems alright to me and doesnt allow any issues but then I would either change the access to the method (and only for the sake of testing it doesn't justify that in my opinion, right?) or to use reflection (is that a good idea, it does not feel right somehow, do people usually use reflection for junit testing?). So the only thing that cant be ruled out is code smell. But may be my code sinus is not really helping me So I would like to understand if I can improve this code.