views:

26

answers:

1

Is it just hype about testing Java classes with Groovy rather than using Mockito, Easymock etc? What's your experience and ease of application?

+2  A: 

Groovy is not a replacement for a Mock framework if that is what you are looking for. In fact, that is why there are mock frameworks written for Groovy such as Gmock. What groovy does well though is provide an easier syntax for writing unit tests. Here are a couple of advantages to using groovy for testing.

  • Less code for your unit tests
  • More readable code for your tests
  • Simpler unit tests
  • Easier to create mock objects even without a framework

Coming from Java, you should find Groovy pretty easy to use. You start with almost the same syntax and then start adding in sugar. First drop the semicolons, next start using property access instead of accessor methods, next you will collapse properties into the shorthand constructor format. Threading is especially more convenient in Groovy for load testing.

Imagine a class call Plop which has a property called name. You could test it's concurrently pretty easily in groovy like this:

def p = new Plop(name: "namehere")

def threads = []
50.times {
    threads << new Thread({
        p.doSomething()
    })
}
threads.each {it.start()}
threads.each {it.join()}
Chris Dail