hibernate - grails IntegrationSpec:如何使其不回滚

标签 hibernate grails gorm integration-testing spock

我想在测试代码下面不要在测试完成后回滚。

我在mysql上插入了数据,但从未提交。

我使用了 @Rollback 和许多东西,但都没有成功。

测试代码如下。

import grails.test.spock.IntegrationSpec
import grails.transaction.Rollback;
import org.hibernate.SessionFactory
import spock.lang.*
import com.ms.domain.WordInSentence

class CompanyServiceSpec extends IntegrationSpec {

    CompanyService companyService;

    void "count login to private area"() {
        setup:''
        when:''
        then:''
//          def transaction = sessionFactory.currentSession.beginTransaction()
            companyService.scoreCompany();
//          transaction.commit()
    }
}

服务类。
@Transactional
class CompanyService {

    def dataSource;

    void scoreCompany() {
        WordInSentence wis = new WordInSentence()
        wis.word = "a"
        wis.location = 1;
        wis.sentence_id = 1
        wis.save(flush:true);
        wis.save();
    }
}

最佳答案

引用Grails Documentation

Integration tests run inside a database transaction by default, which is rolled back at the end of the each test. This means that data saved during a test is not persisted to the database. Add a transactional property to your test class to check transactional behaviour:

class MyServiceTests extends GroovyTestCase {
    static transactional = false
    void testMyTransactionalServiceMethod() {
        …
    }
}


这应该可以回答您的问题
...
class CompanyServiceSpec extends IntegrationSpec {

static transactional = false
...

关于hibernate - grails IntegrationSpec:如何使其不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27705582/

相关文章:

hibernate - 我可以在 hibernate 实体中使用ClosedRange <T>吗?

ajax - 如果浏览操作,则在Grails中重定向用户

grails - 通过引用使用grails/groovy类

grails - Grails-将hasMany关系从一个域对象复制到另一个域对象?

MySQL Workbench 模型到 Java 持久性实体

java - @OneToOne 或 @ManyToOne 引用未知实体

java - JPA/Hibernate flatten mapping(只想得到孙子,跳过 child )

grails - Grails:以多对多关系添加数据会产生错误

hibernate - Grails:使用Where查询选择具有给定id(子查询)的Child of Parent的子代

sql-server - 此后,在SQL Server分析 session 中,将已编译的准备好的语句与exec进行协调