Unit testing
Grails supports the concepts of unit and integration testing. Unit testing is for small focused, fast loading tests that don't load supporting components. Integration testing is for tests that load the surrounding environment.
Grails also supports functional testing.
This documentation applies to Grails 0.5.5 and above which was given a significant overhaul to improve the unit testing capability. See previous revisions for older documentation.
|
Testing is a broad subject which cannot be covered in depth here. However the basic premise is: unit testing should not involve calling any real "collaborator" code, it calls only the specific code you want to test. Integration tests involve calls to other components of your application, and test that everything is "glued together" properly, as your unit tests have already "proven" that the individual parts work. |
Creating a Unit test
To create a unit test run the grails create-unit-test command, which will place the file in <project>/test/unit. Below is an example of completely trivial unit test:
class FooTests extends GroovyTestCase { void testBar() { assert 1 != 2 } }
Creating a Integration test
An integration test, unlike a unit test, provides you with access to the dynamic methods and properties injected by Grails which would otherwise have to be mocked with a mock library. Mocking should be used in unit tests rather than turning your test into an integration test just for the sake of it.
For example unit tests are not able to access domain class dynamic methods like validate(), findAllByXXX and so on. Integration tests bootstrap the Grails environment for your application and as a result take longer to run.
To create an integration test run the grails create-integration-test command, which will place the file in <project>/test/integration.
class BookTests extends GroovyTestCase { void testValid() { def b = new Book(title:"Groovy in Action", author:"Dierk Koenig") assert b.validate() } }
Running Tests
Tests can be run by using the grails test-app command. This will produce output such as:
------------------------------------------------------- Running Unit Tests... Running test FooTests...FAILURE Unit Tests Completed in 464ms ... ------------------------------------------------------- Tests failed: 0 errors, 1 failures
To run an individual test, type the name of the test, without the "Tests" suffix, after the test-app command. For example:
grails test-app Foo
The above will run the test/unit/FooTests.groovy test mentioned at the top of the page.
Note: Since 1.0-RC1 tests can be grouped into sub-folders, however the test class name must be unique across both integration and unit tests. Secondly, the tests are currently parsed as scripts and as such package declarations will cause compilation errors.
Note: The data for all the referenced domain classes is deleted between tests so that tests can be run independently without needing to manage a common state for the database.
Testing Different Artifact Types
- Testing Controllers
- Testing Tag Libraries
- Testing Services
- Testing Domain Classes
- Testing Codecs

