Code Free World

How to setup Groovy + Eclipse + Junit4?

I am working on a small webapp and I want to use Groovy to write some unit testing for my app. Most of my coding is done on Eclipse and I really want to run all the unit testing with the graphical test runner within Eclipse (I really like the green bar :) )

Sadly, after 4 hours of try-and-error, I'm still not able to setup properly. I tried to use the Eclipse Junit4 test runner to run a Groovy file with method annotated for testing using @Test. But it keeps complaining NoClassDefFoundException

Anyone can help?

Here is content of my groovy file, named simpleTest.groovy

import org.junit.Test
import static org.junit.Assert.assertEquals

class simpleTest{
  void trial(){
    assertEquals 6, 3+3

Anyone can help?

What is the NoClassDefFound error you are receiving? That may help determine what is going on.

Unfortunately, the Groovy Eclipse plugin is pretty horrible at giving actual helpful information to let you know what is going wrong with your setup. I'm going to assume you already did the verification to make sure the plugin is actually building your Groovy files (i.e. doing a sample with no dependencies, checking the properly output directory, etc...) After that, it's a lot of really small configuration verification...I've run into problems where the particular "runner" I'm using in Eclipse (i.e. in the Run menu) doesn't have the write class name defined there or for some reason my project didn't get the JUnit library dependency properly inserted into it.

Ultimately, it can be a configuration headache, but long term you'll end up saving some time and gaining some cool functionality if you can knock it out...

I have this working in my environment so here is a brief summary of what I have:

In the run dialog under JUnit:

  • Test Tab: The test class, this must have already been compiled by the Groovy plugin.
  • Classpath: All of the Jar files from my project as well as the Groovy Libraries library

In Window->Preferences->Java->Build Path

  • Classpath Variables:
    = the location where the Groovy plugin is installed

That does the trick for me.

You might want to give the updated plugin a try, see the the recent blog post for more details. The theme of this alpha release is providing an optimized edit/save/compile/test experience, which seems to be your exact use case.

Groovy Eclipse 2.0

I had faced a similar issue and it was the missing package statement that caused me to have problems. Groovy Eclipse plugin did not complain about it but my class was present in a package. I got the noClassDefError when running the file as a JUnit Test.

Adding the package statement to top of class solved this issue.