jeudi 21 avril 2016

Refactoring to expose private methods for unit testing

I have a class that process a string and does many actions on it turn by turn.

Those actions need to be done in a specific order.

Example:

public String process(String stringToProcess) throws Exception {

    processedString = process1(processedString);
    processedString = process2(processedString);
    processedString = process3(processedString);
    processedString = process4(processedString);
    ...
    processedString = process10(processedString);
    ...

    return processedString;
}

I'm sure there is a better way to refactor this to make it cleaner, but most of all, I want to be able to unit test process1, process2 etc.

Note that process1, 2 etc... can be whether 2 line methods or big ones.

Is there a design pattern that would achieve this?

I thought about using enums to achieve this, but I thought it was somehow making it more confusing. This implementation is not the best for sure, but at one glance, you know what's happening.

Edit: This approach is not too bad to me, but all the process methods should be private. And I want to unit test them. I saw this question, so I can test them even if private, but the first step is to make sure it couldn't be done better.

Aucun commentaire:

Enregistrer un commentaire