I suggest refactoring.
The information I reviewed suggests that wanting to try out private methods may be the smell of code. Some people suggest that this may be a sign that it needs to be converted into a separate class and made public.
You have considered various reasons and against this in your own question, you seem to know the arguments well. But you have a method that seems rather complicated and includes an external API. It is worth checking out on your own. removeInvalidOperations() can still be a private method in the class it is in, but it will basically delegate another dependency.
class YourClass { private OperationRemover remover; public void addKeywords() {
This gives you the added benefit that you can replace this dependency at some point, including the ability to test your addKeywords() method without actually placing an external API call, which will make testing this method easier. OperationRemover can be an interface, for example, and for testing purposes, you just pass the stub instead, rather than the specific version used in the production process. As for your specific version, you can write tests for it no matter what happens to your existing class.
I really don’t understand why I have a problem with my current code design, but I also don’t like the idea of editing my production code to suit my requirements. tests.
Lighter testability is a side effect. Look at it differently: what you are actually doing makes the code loosely coupled and extensible. Above, we have separated the call from the external API from the code, which may require the use of the result. The external API is subject to change. You can go from one service to another, but code that uses the result should not care. This class can remain unchanged, only the class that actually places calls must be modified (or replaced).
Real world example: Year 2007, and you work at a bank in a large financial center in the United States. The application must use account information. Your code gets to any web service inside the bank and receives the necessary information in the form that it needs, and then continues its processing. In 2008, the US financial sector is unleashed, and your bank (which is on the verge of collapse) is absorbed by another bank. Your application has been spared, but now you need to turn to another API that already exists in the surviving bank in order to get account information. Should the code that consumes this account information be changed? Not necessary. This is the same account information as before, only from a different source. No, all that needs to be changed is the implementation that calls the API. Consumption code should never know.
The fact that such free communication also contributes and facilitates testing is a bonus.
Anthony Pegram Jan 25 '12 at 2:12 2012-01-25 02:12
source share