Skip to main content

Groovy Mocks and Java Classes

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.

Comments

Post a Comment

Popular posts from this blog

Groovy and String Comparisons

Though I've recently come to love working with Groovy, I'm deep enough where I'm starting to see some of its warts. The one that hit me nearly right away was the nastiness about groovy.lang.GString not extending java.lang.String .  This is mainly the fault of the latter being final and not extendable, but it mea ns that GString in stances aren't automatically comparable with plain Java strings. In Groovy, string literals can be quoted using both single and double-quotes.  GString appears when you use double-quotes around a string and expand variables into it.  See " Strings and GStrings " for more information.

Java command line app with Spring and Gradle

Many Java developers have had to build a command line app for one reason or another over the years, and there are many(!) ways to do it.  There are a bevy of command line libraries, some people roll their own when they get to this point (which is one reason why there are so many command line libraries!), and others just pull in the arguments simply and directly, eschewing a library. However, writing a tool, a developer should just focus on the meat of the problem rather than having to worry about the enabling functionality that allows it to work.  The Spring Framework very much supports this approach to development with its inversion of control (IoC) container and a myriad of other features. To support that approach for command line applications, the spring-cmdline library provides a bridge between the Spring framework and the JOpt Simple command line library. This tutorial will show how to leverage the spring-cmdline library to stand up a very simple command line ap...

Family budget crisis prompts parents to cut back on work?

Minnesota's Governor Tim Pawlenty has previously used a state-as-family metaphor , saying that when a family is stuck with a budget deficit, they have no choice but to tighten their belts.  This is not quite true, as it ignores the income side of the equation.  Most families I know have tried to acquire more jobs or longer hours in order to increase income.  Some have even invested in something that might pay off in higher income, like going back to school to finish a degree or get additional training.  Looking only at decreasing expenses is considering only half of the problem. The good news is that Governor Pawlenty now agrees that taxes may enter into the equation.  The bad news is that he's taken a George W. Bush approach to it, advocating lowering taxes to bring Minnesota out of its current financial mess.  My mind boggles at the logic.