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