How to perform tests on private methods using Mockito

1

This weekend I read about Mockito and trial stunts, and the following idea came to me. Test private methods with this framework.

I see that PowerMock performs this procedure, but has anyone tried to do this using Mockito with reflection ?

In this discussion I would also like to know from friends the opinion about whether or not to test private methods, I saw some discussions about it in SO.com and I was in doubt, because when I declare a private method only segment for each one method a responsibility and do not put several things in the same method.

Thank you

    
asked by anonymous 18.08.2014 / 15:31

1 answer

3

Apparently it is not possible with Mockito, but there is PowerMock, a framework that extends other frameworks like Mockito, and allows test private methods .

However, in general, if a private method has some logic or algorithm that is complex enough to require unit testing by itself, then it may be advisable to delegate responsibility to another class and then test it.

Another possibility, which sacrifices a little design , would be to make the method "protected" ( protected ) so that other classes in the same package can access it. If there is adequate splitting of packets into the system this is usually not a problem. So just create the test in the same package.

In fact, often the level of visibility private ends up being "strong" too much. I have seen many cases where I needed to extend a class and an important method was private, forcing me to create another class or duplicate the method.

    
19.08.2014 / 15:54