• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Start testing your Java with Groovy

 
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's look at an example of how using Groovy to test your existing Java code could cause significant gains...

So you have this nasty interface that needs to be mocked and injected into your class under test:

ComplicatedInterface.java

In order to create a mock or stub object for this, you'll need to implement three methods, one with a ResultSet, and one with several parameters. Bleck.

The class under test is called Worker:

Worker.java

... and all we really want to test is that uglyMethod calls some static utils class with the given interface implementation twice, then compares the results properly, returning true if the second number is greater than the first. For simplicity's sake, we'll make our static utils as dumb as possible:

DumbUtils.java

So if you want to test Worker.uglyMethod using Java, it would be messy. You'd have to create a mock class that implements the ComplicatedInterface, and it would have all those nasty method implementations. I know you've seen some interfaces with 17 required methods, each one more complicated than the other. I'm making it even simpler just by having three.

But let's not even bother to do this with Java. Let's do it the easy way: with Groovy...

WorkerTests.groovy

In case you missed it, the magic line is

We know the class under test needs to call DumbUtils.generateNumber, passing in the ComplicatedInterface. In DumbUtils, the interface's methodThree is called to return an int value. So we've created a mock method in the form of a closure. In the first test, it starts at 0 and returns one greater on each call. In the second test, it goes one lesser. We are using this closure within a map to provide an implementation of the ComplicatedInterface's methodThree requirement. And because we are telling Groovy that we want to treat this map "as" a ComplicatedInterface, it takes care of mocking out all the rest of the interface's implementation. Then that mock is injected into the class under test and is available during the test run.

This is a common antipattern in enterprise applications, and it can be frustrating to keep the tests focused on the code under test, instead of mocking out the state of the application to prepare for the test. Groovy can help provide shortcuts during testing that makes your tests more expressive and help you focus on the real task of your tests.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I love this post. This testing trick is something I've used quite a bit.

In some cases, this can eliminate the need for an interface declaration altogether. Spring's popularity has led to an interface being created for nearly every bean even though the only two implementations are typically the actual implementation and the test implementation. Eliminating the need for the test implementation eliminates the need for a separate interface declaration (why have an interface with only one class that implements it?). That cleans up the package noise in my projects quite a bit and I like that.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic