Testing 如何在特定方法中防止@Rollback?

Testing 如何在特定方法中防止@Rollback?,testing,grails,spock,Testing,Grails,Spock,我正在使用Grails3中的Spock进行测试。一个特定的测试用例正在崩溃,因为Grails正在两个不同的会话中与我的数据库对话。我的规范用@Rollback注释,以回滚每次测试所做的所有更改 是否有一种方法可以禁用此方法的@Rollback,然后在测试完成后手动回滚更改 更新 下面是我的测试规范的简化示例: @Log4j @Integration @Rollback class PublicationReportBreakingSpec extends Specification {

我正在使用Grails3中的Spock进行测试。一个特定的测试用例正在崩溃,因为Grails正在两个不同的会话中与我的数据库对话。我的规范用
@Rollback
注释,以回滚每次测试所做的所有更改

是否有一种方法可以禁用此方法的
@Rollback
,然后在测试完成后手动回滚更改

更新

下面是我的测试规范的简化示例:

@Log4j
@Integration
@Rollback
class PublicationReportBreakingSpec extends Specification {

    @Unroll
    @Rollback
    void "Invalidate #type object and check not in report"(type, status, hits, expect)
    {
        //We want to roll back the changes made to the publication after each pass.

        given: "Find a valid #type publication"
        //Finding publication which is Read (valid, should appear in report)
        final Publication pub = getSinglePub(type, status, hits)

        //Set publication to be Unread (invalid, shouldn't appear in report)
        when: "The #type publication is altered to fail"
        pub.setStatus('Unread')
        pub.save(flush:true, failOnError: true)

        then: "Check the properties match the test case"
        pub.status = 'Unread'

        //Generate report of read publications
        when: "The report is run"
        def resp = PublicationController.reportReadPublications()

        //Make sure report doesn't contain the publication
        then: "Check for expected result #expect"
        checkReportExpectedResult(resp, expect)

        where:
        clause                                            | type       | status   | hits || expect
        "Book is unread"                                  | 'Book'     | 'Read'   | 1200 || 0
        "Article is unread"                               | 'Article'  | 'Read'   | 200  || 0
    }

    //Checks report for expect value
    public void checkReportExpectedResult(resp, expect){...} 

    //Returns single publication based on passed in parameters
    public Publication getSinglePub(type, status){...}
}
错误的堆栈跟踪是:

<testcase name="Testing breaking domain class changes. Book is unread" classname="com.my.project.PublicationReportBreakingSpec" time="118.216">
<failure message="java.lang.IllegalStateException: No transactionManager was specified. Using @Transactional or @Rollback requires a valid configured transaction manager. If you are running in a unit test ensure the test has been properly configured and that you run the test suite not an individual test method." type="java.lang.IllegalStateException">java.lang.IllegalStateException: No transactionManager was specified. Using @Transactional or @Rollback requires a valid configured transaction manager. If you are running in a unit test ensure the test has been properly configured and that you run the test suite not an individual test method.
at grails.transaction.GrailsTransactionTemplate.&lt;init&gt;(GrailsTransactionTemplate.groovy:60)
at com.my.project.PublicationReportBreakingSpec (Removed due to sensitivity)
at com.my.project.PublicationReportBreakingSpec (Removed due to sensitivity)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:430)
at grails.transaction.GrailsTransactionTemplate$2.doInTransaction(GrailsTransactionTemplate.groovy:96)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at grails.transaction.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:93)
at com.my.project.PublicationReportBreakingSpec.Invalidate #type object and check not in report. Check #clause, publication type #type(PublicationReportBreakingSpec.groovy)

java.lang.IllegalStateException:未指定transactionManager。使用@Transactional或@Rollback需要有效配置的事务管理器。如果在单元测试中运行,请确保测试已正确配置,并且运行测试套件而不是单个测试方法。
位于grails.transaction.grailTransactionTemplate.init(grailTransactionTemplate.groovy:60)
在com.my.project.PublicationReportBreakingSpec上(由于敏感性而删除)
在com.my.project.PublicationReportBreakingSpec上(由于敏感性而删除)
调用(Closure.java:414)
调用(Closure.java:430)
位于grails.transaction.grailTransactionTemplate$2.doInTransaction(grailTransactionTemplate.groovy:96)
位于org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
位于grails.transaction.grailTransactionTemplate.execute(grailTransactionTemplate.groovy:93)
在com.my.project.PublicationReportBreakingSpec.Invalidate#键入对象并选中不在报告中。检查#子句,发布类型#类型(PublicationReportBreakingSpec.groovy)

根据注释,使用
@Rollback(false)
进行测试应该可以达到目的。

根据注释,使用
@Rollback(false)
进行测试应该达到目的。

如果
@Rollback(false)
不起作用,可能会出现问题


作为一种变通方法,注释也可以放在方法级别。因此,从测试类中删除注释并将其放在测试方法中,但不想回滚的方法除外。然后在该规范中添加一个
清理:
部分来清理您创建的数据。

如果
@Rollback(false)
不起作用,可能会出现问题


作为一种变通方法,注释也可以放在方法级别。因此,从测试类中删除注释并将其放在测试方法中,但不想回滚的方法除外。然后在该规范中添加一个
清理:
部分来清理您创建的数据。

如果您对回滚更改不感兴趣,则不希望使用事务。。。因此,您可以跳过该方法中的事务。您可以使用
@NotTransactional

执行此操作。如果您对回滚更改不感兴趣,则不希望使用事务。。。因此,您可以跳过该方法中的事务。您可以使用
@NotTransactional

来实现这一点,我已经尝试过了,使用该特定方法上的注释,我得到了一个异常
groovy.lang.MissingPropertyException:没有这样的属性:class:org.grails.transaction.GrailTransactionAttribute
的值。我还尝试了
@Rollback(value=false)
,这导致了相同的错误。我添加了一个代码示例和stacktrace。由于敏感性,它被大量修改,但我相信我已经捕捉到了最重要的部分。我尝试过,在该特定方法上使用注释,得到了一个异常
groovy.lang.MissingPropertyException:没有这样的属性:类的值:org.grails.transaction.GrailTransactionAttribute
。我还尝试了
@Rollback(value=false)
,这导致了相同的错误。我添加了一个代码示例和stacktrace。由于灵敏度的原因,它进行了大量修改,但我相信我已经捕获了最重要的部分。你不能回滚典型的Geb测试。您无法控制该事务,因为它发生在另一个线程上好的,这是有道理的。所以使用@Rollback是多余的,即使它编译了?那我怎么做测试呢?我正在更改一个域对象并将其保存到数据库中,但看起来好像另一个会话正在运行测试用例,这导致它失败,并且它没有拾取对该对象所保存的更改。我正在对我所做的任何更改进行
.save(flush:true)
。编辑:对不起,我刚刚注意到帖子中有一个打字错误。我使用Spock而不是Geb进行测试。编辑:我添加了一个代码示例和stacktrace。由于灵敏度的原因,它进行了大量修改,但我相信我已经捕获了最重要的部分。你不能回滚典型的Geb测试。您无法控制该事务,因为它发生在另一个线程上好的,这是有道理的。所以使用@Rollback是多余的,即使它编译了?那我怎么做测试呢?我正在更改一个域对象并将其保存到数据库中,但看起来好像另一个会话正在运行测试用例,这导致它失败,并且它没有拾取对该对象所保存的更改。我正在对我所做的任何更改进行
.save(flush:true)
。编辑:对不起,我刚刚注意到帖子中有一个打字错误。我使用Spock而不是Geb进行测试。编辑:我添加了一个代码示例和stacktrace。由于灵敏度的原因,它进行了大量修改,但我相信我已经捕捉到了最重要的部分。