Grails 3.3.11中的集成测试不会回滚

时间:2020-09-25 22:09:18

标签: grails

我正在使用Grails 3.3.11,集成测试有问题。碰巧测试已经完成,一切都很好,但是数据正在数据库中累积。我已经尝试了几件事,例如使用@Rollback批注,但是这没有发生。数据库中有很多垃圾。你能帮助我吗?我究竟做错了什么。我无法停止将数据保存在数据库中的测试。我希望回滚是自动的。

import org.springframework.test.annotation.Rollback

@Integration
@grails.transaction.Rollback
class CmbIdControllerSpec extends Specification {
    @Value('${local.server.port}')
    Integer serverPort
    String accessToken
    String baseUrl
    JSONObject documentPropertiesForTesting
    JSONObject documentForTesting
    String partTest
    String userTest
    String typeIdTest
    String documentIdTest
    def sessionFactory
    def controller

    void setup(){
        baseUrl = "http://localhost:${serverPort}/cmbid/api/v1"
        partTest = "partTest"
        accessToken = "Bearer examplefXk"
    }

    void "Saving a new and valid document properties"() {
        given:
            documentPropertiesForTesting = createNewTestDocumentProperties()
            typeIdTest = documentPropertiesForTesting.get("message").toString().substring(20,52)

        expect:
            documentPropertiesForTesting.get("status") == "ok"
            documentPropertiesForTesting.get("message").contains("properly saved!")

    }

@Rollback
def createNewTestDocumentProperties(){
    CloseableHttpClient httpClient = HttpClients.createDefault();
    def httpPost = setHttpPost(baseUrl + "/parts/${partTest}/metas")

    JSONObject entireBody = new JSONObject()
    entireBody.accumulate("highlights",setHighlightsDP())
    entireBody.accumulate("screens", setScreensDP())
    entireBody.accumulate("photo", "")
    entireBody.accumulate("style", setStyleDP())

    StringEntity jsonBody = new StringEntity(entireBody.toString())
    httpPost.setEntity(jsonBody)

    CloseableHttpResponse httpResponse = httpClient.execute(httpPost)
    JSONObject responseBody = new JSONObject(EntityUtils.toString(httpResponse.getEntity()))

    httpClient.close()

    return responseBody
}

1 个答案:

答案 0 :(得分:0)

呃...你为什么有这个:

@Rollback
def createNewTestDocumentProperties(){

由于您的初始注解声明为“ @ grails.transaction.Rollback”,因此“规范”应正确处理每个测试的回滚。这个中学班很可能需要搬出。