Exposing a property for testing only
Hi;
I need to expose a property in a class for testing only. Any ideas on how to do it?
I don't want to have a testMe() method in the class because then junit is required with the application even though it is never called.
I don't want to have a public property because no matter what the docs say - people will call the method.
? - thanks - dave
[380 byte] By [
DavidThia] at [2007-10-1 12:32:41]

> I need to expose a property in a class for testing
> only. Any ideas on how to do it?
You could give the property package-only visibility, and put the test in the same package. This doesn't expose it for testing only, but it does reduce the visibility.
> I don't want to have a testMe() method in the class
> because then junit is required with the application
> even though it is never called.
Eh? Why would that require junit? Just because you expose a method it doesn't mean that you need to include code that invokes it. Unless you meant that the testMe method would invoke junit.
But you could include a test method that tested itself and returned true or false without invoking junit; then junit would just invoke that method.
> Hi;
>
> I need to expose a property in a class for testing
> only. Any ideas on how to do it?
java.lang.reflect.AccessibleObject.setAccessible(boolean flag)
>
> I don't want to have a testMe() method in the class
> because then junit is required with the application
> even though it is never called.
>
> I don't want to have a public property because no
> matter what the docs say - people will call the
> method.
That hasn't been my experience.