Wednesday, 18 July 2007

Unittesting private methods

I found something that throws an exception in a private method, to make sure I correct it, and it keeps corrected, I would like to write a couple of tests for that method. But its private, so how do I go..

I read the following article, which do describe it for Java, but the same problems apply for (except I dont know if .net reflection allows one to access private methods like java)
Testing Private Methods with JUnit and SuiteRunner

As you will see, there is no nice way to do it, so I think I will try to rethink the design, and then see if I really need to test a private method.

Finishing with a quote:
In general, you don't want to break any encapsulation for the sake of testing (or as Mom used to say, "don't expose your privates!")

By Dave Thomas and Andy Hunt, in their book Pragmatic Unit Testing.

No comments: