Tuesday, September 20, 2011

Groovy Junit tests in Eclipse Indigo

I'm playing with Groovy and Grails a little bit and recently I had a problem running unit-test from recent Eclipse Indigo (3.7) with groovy-eclipse plugin installed.

Problem manifests itself with following error, when I try to righ-click and "run as" junit test-case.
junit.framework.AssertionFailedError: No tests found in path.to.your.class

I've spent some time trying to find solution and it was obvious. Think it might be helpful to keep solution in some form. I'll probably post more Groovy/Grails related stuff in the future, so why not start with this.

Let's have simple class MyClass
package sk.adino.poc.groovy.unittests

class MyClass {
  x() { return 1;}
}

and test class MyClassTest in same package

package sk.adino.poc.groovy.unittests

class MyClassTest extends GroovyTestCase {
  testX { assert x==1 }
}

Right-click in eclipse and run as JUnit test should work, but you have to:
  1. have groovy-eclipse plugin installed
  2. add external JAR pointing to junit-x.y.z.jar from your groovy installation lib directory - otherwise it won't even compile
  3. add void as a return type of your testX function so it should look like:
package sk.adino.poc.groovy.unittests

class MyClassTest extends GroovyTestCase {
  void testX { assert x==1 }
}

No comments: