2

I've tried new functional tests in Grails 3. It test a simple REST API, create a POST request and then observe if new resource was created.

import geb.spock.GebSpec
import grails.converters.JSON
import grails.test.mixin.integration.Integration
import grails.transaction.Rollback
import groovy.util.logging.Slf4j
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity

@Integration
@Rollback
@Slf4j
class QuestionFunctionalSpec extends GebSpec {
    void "POST  to /questions creates new question"() {
        setup:
        log.debug("Existing questions: ${Question.list().collect { [id: it.id, text: it.text] }}")
        def jsonRequest = [text: "How to cook for people?"] as JSON

        when: "The api call is made"
        def httpPost = new HttpPost("localhost:8080/api/v1/questions")
        httpPost.setEntity(new StringEntity(jsonRequest.toString()));
        HttpClients.createDefault().execute(httpPost)

        then: "New question was created"
        //log.debug("Questions after test (first domain class call): ${Question.list().collect { [id: it.id, text: it.text] }}")
        //log.debug("Questions after test (second domain class call): ${Question.list().collect { [id: it.id, text: it.text] }}")
        assert Question.list().size() == 1
    }
}

Locally everything worked well but running this on my CI server, I've got my test fail. Uncommenting the first log.debug() I got a message that no Question resource is in database, however the test didn't fail. Uncommenting the second log.debug(), I've seen that a Question resource is actually there but it is available just with a second domain class call.

Am I missing here something. My suspicion was that Apache HttpClient is not synchronous but it actually is. Do you have similar experience? Is there any synchronization problem in Geb?

Thanks, Mateo

kuceram
  • 3,795
  • 9
  • 34
  • 54
  • Why do you extend `GebSpec`? You're not using any part of Geb if you're sending a request to the application using `HttpClient`. – erdi Sep 05 '15 at 18:14
  • I use it in some other test features, here I test only REST API. However, this doesn't answer my question. I recently found out that you should use remote control plugin to access GORM, I should not rely on same JVM, http://stackoverflow.com/questions/28505134/invoking-gorm-methods-from-geb-tests – kuceram Sep 07 '15 at 09:33
  • Yes, because tests and the application under test run in different JVMs when running functional tests. – erdi Sep 07 '15 at 09:37

1 Answers1

0

I found out that GORM should not be accessed directly in functional tests as the test itself might potentially run in separate JVM (invoking GORM methods from Geb tests).

Appropriate would be to use a remote-control plugin (https://grails.org/plugin/remote-control). However, it doesn't have support for Grails 3 yet (https://github.com/alkemist/grails-remote-control/issues/27).

Community
  • 1
  • 1
kuceram
  • 3,795
  • 9
  • 34
  • 54