In this post however I do not want to start a discussion on this topic but show how it can be done easily in Groovy and Java. Let's have a look at the following code:
public class SomeClass {
private String methodToBeTested(String input) {
return input.toLowerCase();
}
}
// inside of a test
SomeClass someClass = new SomeClass();
assert "foo" == someClass.methodToBeTested("Foo");
If you run this code in Groovy you may expect an Error because the test calls a private method of SomeClass. However as of Groovy 2.1.9 this is currently not the case due to a bug in Groovy:
http://jira.codehaus.org/browse/GROOVY-1875
Consequently testing private methods in Groovy currently is really simple: just do it.
However if
To overcome this testing problem one can use the Java Reflection API.
The test code must be changed like this:
Note that the reflection methods can throw several Exceptions. If the test is written in Java these must be caught or the test method must be adjusted accordingly.
http://jira.codehaus.org/browse/GROOVY-1875
Consequently testing private methods in Groovy currently is really simple: just do it.
However if
SomeClass
is a class produced by the Java compiler the test will fail with an error like this:methodToBeTested(java.lang.String) has private access in SomeClass
someClass.methodToBeTested("Foo");
The Groovy developers are working on a fix for the issue that will hopefully make it into Groovy 3.
So the Groovy code shown above is eventually not compatible with future Groovy releases.To overcome this testing problem one can use the Java Reflection API.
The test code must be changed like this:
// inside of a test
SomeClass
someClass = new SomeClass();
Method method = SomeClass.class.getDeclaredMethod("methodToBeTested", String.class)
method.setAccessible(true);
assert "foo" == method.invoke(someClass, "Foo");
Note that the reflection methods can throw several Exceptions. If the test is written in Java these must be caught or the test method must be adjusted accordingly.