【发布时间】:2015-09-07 17:41:35
【问题描述】:
我在 Grails 3 中尝试了新的功能测试。它测试一个简单的 REST API,创建一个 POST 请求,然后观察是否创建了新资源。
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
}
}
本地一切正常,但在我的 CI 服务器上运行它,我的测试失败了。取消注释第一个 log.debug() 我收到一条消息,指出数据库中没有 Question 资源,但是测试没有失败。取消对第二个 log.debug() 的注释,我发现问题资源实际上存在,但仅通过第二个域类调用即可使用。
我在这里错过了什么吗?我怀疑 Apache HttpClient 不是同步的,但实际上是同步的。你有类似的经历吗? Geb有同步问题吗?
谢谢, 马特奥
【问题讨论】:
-
为什么要扩展
GebSpec?如果您使用HttpClient向应用程序发送请求,则您没有使用 Geb 的任何部分。 -
我在其他一些测试功能中使用它,这里我只测试REST API。但是,这并不能回答我的问题。我最近发现你应该使用远程控制插件访问GORM,我不应该依赖同一个JVM,stackoverflow.com/questions/28505134/…
-
是的,因为在运行功能测试时,测试和被测应用程序在不同的 JVM 中运行。
标签: grails spock geb grails-3.0