Trying to convert a Java/EasyMock based JUnit test to Groovy in the last couple days led me to find what I think is a bug in Groovy's MockFor class.
The documentation for MockFor says to use the proxyDelegateInstance method when testing Java classes, but unfortunately the delegate proxy obtained using that method is type-incompatible with the Java class you're trying to mock if that class is concrete. It works for Groovy classes, all abstract classes, and all interfaces, but it appears to fall down when given a plain, concrete Java class.
Take this contrived Java class:
A simple collaborating class:
And a JUnit that mocks JavaClass to test CollaboratingClass:
This test will fail under Groovy 1.8.6 because the javaClassInstance won't actually be type compatible with Java class!
Wrapping that class in an abstract class or a Groovy concrete class works around the issue, but that adds some unnecessary complexity to using Groovy to test existing Java classes.
The documentation for MockFor says to use the proxyDelegateInstance method when testing Java classes, but unfortunately the delegate proxy obtained using that method is type-incompatible with the Java class you're trying to mock if that class is concrete. It works for Groovy classes, all abstract classes, and all interfaces, but it appears to fall down when given a plain, concrete Java class.
Take this contrived Java class:
A simple collaborating class:
And a JUnit that mocks JavaClass to test CollaboratingClass:
This test will fail under Groovy 1.8.6 because the javaClassInstance won't actually be type compatible with Java class!
Wrapping that class in an abstract class or a Groovy concrete class works around the issue, but that adds some unnecessary complexity to using Groovy to test existing Java classes.
This comment has been removed by a blog administrator.
ReplyDelete