15.2 Integration Testing

Integration tests differ from unit tests in that you have full access to the Grails environment within the test. You can create an integration test using the create-integration-test command:

  1. $ grails create-integration-test Example

The above command will create a new integration test at the location src/integration-test/groovy/<PACKAGE>/ExampleSpec.groovy.

Grails uses the test environment for integration tests and loads the application prior to the first test run. All tests use the same application state.

Transactions

Integration test methods run inside their own database transaction by default, which is rolled back at the end of each test method. This means that data saved during a test is not persisted to the database (which is shared across all tests). The default generated integration test template includes the Rollback annotation:

  1. import grails.testing.mixin.integration.Integration
  2. import grails.gorm.transactions.*
  3. import spock.lang.*
  4. @Integration
  5. @Rollback
  6. class ExampleSpec extends Specification {
  7. ...
  8. void "test something"() {
  9. expect:"fix me"
  10. true == false
  11. }
  12. }

The Rollback annotation ensures that each test method runs in a transaction that is rolled back. Generally this is desirable because you do not want your tests depending on order or application state.

In Grails 3.0 tests rely on grails.gorm.transactions.Rollback annotation to bind the session in integration tests. Though each test method transaction is rolled back, the setup() method uses a separate transaction that is not rolled back.Data will persist to the database and will need to be cleaned up manually if setup() sets up data and persists them as shown in the below sample:

  1. import grails.testing.mixin.integration.Integration
  2. import grails.gorm.transactions.*
  3. import spock.lang.*
  4. @Integration
  5. @Rollback
  6. class BookSpec extends Specification {
  7. void setup() {
  8. // Below line would persist and not roll back
  9. new Book(name: 'Grails in Action').save(flush: true)
  10. }
  11. void "test something"() {
  12. expect:
  13. Book.count() == 1
  14. }
  15. }

To automatically roll back setup logic, any persistence operations need to be called from the test method itself so that they are run within the test method’s rolled back transaction. Similar to usage of the setupData() method shown below:

  1. import grails.testing.mixin.integration.Integration
  2. import grails.gorm.transactions.*
  3. import spock.lang.*
  4. @Integration
  5. @Rollback
  6. class BookSpec extends Specification {
  7. void setupData() {
  8. // Below line would roll back
  9. new Book(name: 'Grails in Action').save(flush: true)
  10. }
  11. void "test something"() {
  12. given:
  13. setupData()
  14. expect:
  15. Book.count() == 1
  16. }
  17. }

Using Spring’s Rollback annotation

Another transactional approach could be to use Spring’s @Rollback instead.

  1. import grails.testing.mixin.integration.Integration
  2. import org.springframework.test.annotation.Rollback
  3. import spock.lang.*
  4. @Integration
  5. @Rollback
  6. class BookSpec extends Specification {
  7. void setup() {
  8. new Book(name: 'Grails in Action').save(flush: true)
  9. }
  10. void "test something"() {
  11. expect:
  12. Book.count() == 1
  13. }
  14. }
It isn’t possible to make grails.gorm.transactions.Rollback behave the same way as Spring’s Rollback annotation because grails.gorm.transactions.Rollback transforms the byte code of the class, eliminating the need for a proxy (which Spring’s version requires).This has the downside that you cannot implement it differently for different cases (as Spring does for testing).

DirtiesContext

If you do have a series of tests that will share state you can remove the Rollback and the last test in the suite should feature the DirtiesContext annotation which will shutdown the environment and restart it fresh (note that this will have an impact on test run times).

Autowiring

To obtain a reference to a bean you can use the Autowired annotation. For example:

  1. ...
  2. import org.springframework.beans.factory.annotation.*
  3. @Integration
  4. @Rollback
  5. class ExampleServiceSpec extends Specification {
  6. @Autowired
  7. ExampleService exampleService
  8. ...
  9. void "Test example service"() {
  10. expect:
  11. exampleService.countExamples() == 0
  12. }
  13. }

Testing Controllers

To integration test controllers it is recommended you use create-functional-test command to create a Geb functional test. See the following section on functional testing for more information.