Unit Testing Private Methods
So I've always thought that when it comes to writing unit tests against private methods, you have two options; use reflection, or break encapsulation. However, I just read an article that discusses this very topic in a good amount of detail. It points out 4 options for testing private methods.
If you're just interested in a summary of those 4 options, keep reading. Otherwise, here is the article I came across.
1. Don't Test Private Methods
2. Break Encapsulation
3. Use Reflection
4. Use an Inner Class
My $.02
I really don't like the idea of using a nested test class, but I'd love to hear arguments promoting this option. Also, I don't like sacrificing encapsulation for the sake of testing. If you want to test the private method, do it right and use reflection. Other than that, I'm sure indirectly testing private methods has its time and place, but how often is determined by how you write your code.
No comments:
Post a Comment