2017-09-18 39 views
0

我正在Grails 3中使用Spock进行测试。由于在这种情况下,Grails在两个不同的会话中对我的数据库说话,所以特定的测试用例正在中断。我的规范注释了@Rollback,以回滚每个测试所做的所有更改。如何在特定方法中防止@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) 

+0

您无法回滚典型的Geb测试。自从它发生在另一个线程上后,您无法控制交易 –

+0

好吧,这是有道理的。所以使用@Rollback是多余的,即使它编译?那么我怎么做我的测试?我正在更改一个域对象并将其保存到数据库中,但是看起来好像另一个会话正在运行导致它失败的测试用例,并且它不会保存对对象的保存更改。我正在做'.save(flush:true)'我做的任何修改。 – Donglecow

+0

编辑的OP:对不起,我只是注意到了文章中的一个错字。我正在使用Spock而不是Geb进行测试。 – Donglecow

回答

1

按照Javadoc注释@Rollback(false)的测试应该可以做到。

+0

我试过了,在特定的方法上使用了注解,我得到一个异常'groovy.lang.MissingPropertyException:没有这样的属性:类的值:org.grails.transaction.GrailsTransactionAttribute'。我也尝试过'@Rollback(value = false)'这会导致相同的错误。 – Donglecow

+0

你能分享你的代码和完整的堆栈跟踪 –

+0

我已经添加了我的代码和堆栈跟踪的例子。由于灵敏度而进行了大量修改,但我相信我已经捕获了最重要的部分。 – Donglecow

1

这可能是如果@Rollback(false)不工作的问题...

作为一种变通方法,注释可以在方法层面上投入太多。因此,从您的测试类中移除注释并将其放在您的测试方法上,除了您不想回滚的测试方法。然后在该规范中添加一个cleanup:部分来清理您创建的数据。

0

如果您不想回滚您的更改,则不想使用事务...因此您可以跳过该方法中的事务。你可以使用@NotTransactional

相关问题