Subverting Java Access Protection for Unit Testing
Subject:   Alternatives to reflection
Date:   2003-11-14 01:56:04
From:   anonymous2
I have variously used two other approaches to get around the problem of testing private fields/methods:

1. Use static innner classes. These have access to the private fiels/methods of the enclosing class. The restriciton is that they can only access the private variables of one class. However, you can build public "communication" methods into them. These are methods that expose private date of the outer class and are intended for use by other classes that perform tests.

2. Logging. It's a good idea to add logging to an applicaiton anyway. Sometimes I perform a test and check the result of a log (where private variable content appears) to determine success/failure.

Full Threads Newest First

Showing messages 1 through 1 of 1.

  • Alternatives to reflection
    2003-11-14 06:15:01  anonymous2 [View]

    This was a great article. I really hated having to set private methods as package methods just to be able to run the Junit test. I gave it a try and it works great. Many thanks.