2

How to run Grails integration tests against a running server?

I've searched the web but the advices date to 2010 and Grail 1.3.x...

Amio.io
  • 20,677
  • 15
  • 82
  • 117

2 Answers2

0

Grails 3 Integration test will run against a running server. You need to define a test environment application.yml and add the @Integration annotation to your class.

import grails.test.mixin.integration.Integration
import spock.lang.Specification

@Integration
class LookupServiceIntegrationSpec extends Specification {

    def lookupService

    def setup() {
    }

    def cleanup() {
    }

    void "test database connection"() {

        when:
        def row = lookupService.testMethod()

        then:
        println("Row one = "+row.one)
        row.one == 1
    }
}

Here is a simple example service:

import grails.transaction.Transactional
import groovy.sql.Sql

@Transactional
class LookupService {

    def dataSource

    def testMethod() {
        Sql sql = new Sql(dataSource)
        sql.firstRow("SELECT 1 AS one")
    }
}
Joe
  • 1,219
  • 8
  • 13
  • thx for the answer. But how do I attach the to an application running e.g. on `localhost:8080`? – Amio.io Mar 16 '17 at 16:59
  • Your talking about Functional Tests not Integration Tests. – Joe Mar 16 '17 at 18:22
  • I'm talking about both the tests. In Grails functional tests are technically integration tests too as they are also annotated with `@Integration`... but big thank for trying to help me. :D Maybe I should generalize my question: "How can I run any code agains a running Grails server?". Do you see what I need? – Amio.io Mar 17 '17 at 12:01
0

Maybe your looking for Functional Tests not Integration Tests. Grails 3 added better support for Functional Tests. See Link Below.

Grails 3 Functional Tests

Joe
  • 1,219
  • 8
  • 13