Is it bad practice to use Reflection in Unit testing?

Posted by Sebi on Stack Overflow See other posts from Stack Overflow or by Sebi
Published on 2010-05-11T13:40:04Z Indexed on 2010/05/11 13:44 UTC
Read the original article Hit count: 155

Filed under:
|
|
|
|

During the last years I always thought that in Java, Reflection is widely used during Unit testing. Since some of the variables/methods which have to be checked are private, it is somehow necessary to read the values of them. I always thought that the Reflection API is also used for this purpose.

Last week i had to test some packages and therefore write some JUnit tests. As always i used Reflection to access private fields and methods. But my supervisor who checked the code wasn't really happy with that and told me that the Reflection API wasn't meant to use for such "hacking". Instead he suggested to modifiy the visibility in the production code.

Is it really bad practice to use Reflection? I can't really believe that

© Stack Overflow or respective owner

Related posts about testing

Related posts about java