haxx populi

Grails testing

by jja on
Topics:

The Grails docs talk about testing and have some example test methods but fail to describe some simple but necessary mechanics to get it going. The test methods should be in a class that extends GroovyTestCase (that word doesn't appear at all when searching the Grails website). The class name must end with Tests since it must be in a file with a name ending in Tests.groovy under the test directory of your grails project.

For example:

test/unit/FooTests.groovy:
class FooTests extends GroovyTestCase {
    void testOne() {
        assertTrue(1==1);
    }
}

GroovyTestCase is described some by the Groovy docs; it extends Junit's TestCase. Note that Grails 1.0.3 includes the Junit 3.8.2 JAR file. To get docs, go to the Junit website then to JUnit Releases and download and unpack version 3.8.1 (there is no 3.8.2 there; it must be a Grails fix).

If you must, you can also have Java tests (calling Groovy/Grails objects from Java is left as an exercise to the reader):

test/unit/BarTests.java:
import junit.framework.TestCase;
public class BarTests extends TestCase {
    public void testTwo() {
        assertTrue(2==2);
    }
}

Then from the top of your Grails project, just run grails test-app

% grails test-app
...
-------------------------------------------------------
Running 2 Unit Tests...
Running test FooTests...
                    testOne...SUCCESS
Running test BarTests...
                    testTwo...SUCCESS
Unit Tests Completed in 155ms ...
-------------------------------------------------------
...

with more output in the test/report/ directory.

btw, you can change the filename pattern with the config option grails.testing.patterns (undocumented but mentioned on the mailing list).

(comments are closed)