lundi 25 juillet 2016

Is it possible to make a class package-private for testing purposes

I have a class which is going to perform some complex action involving some computation like this:

public class ComplexAction {

    public void someAction(String parameter){
        ComplexActionEvaluator evaluator = new ComplexActionEvaluator();
        int value = evaluator.eval(parameter);
        //some other actions
    }

    static class ComplexActionEvaluator implements Evaluator{
        public int eval(String parameter){
            //evaluation
        }
    }
}

//package-private, uses only for Actions
interface Evaluator{
    int eval(String parameter);
}

Since the evaluator might contain some complex logic I'd like to write tests for it. But since the ComplexActionEvaluator is specific to the ComplexAction I'd do it private rather then package-private. But this would make it unavailable for testing.

The soulution first came to my mind was just to make it default-access. But the default-access is just for getting available for testing.

I think that it's not quite good if the decision on which access modifier to use is biased by testing. Maybe I do something wrong or it's a common practice?

Aucun commentaire:

Enregistrer un commentaire