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]
# 1
You could make the getter/setter methods for the property throw an exception, if junit.jar isn't on the classpath or some other environment variable isn't defined.
sp00kera at 2007-7-10 14:50:46 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 2

> 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.

paulcwa at 2007-7-10 14:50:46 > top of Java-index,Archived Forums,Debugging Tools and Techniques...
# 3

> 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.

jschella at 2007-7-10 14:50:46 > top of Java-index,Archived Forums,Debugging Tools and Techniques...