我目前正在为我们的项目进行一些集成(或功能性,我不是真正的 QA,而是后端开发人员,所以我可能会草率地使用术语)REST 测试,我们正在使用 Grails 3.0.9、Spock Framework 1.0 -Groovy-2.4 和 PostgreSQL DB 用于测试。
此外,我们有单独的数据库用于测试目的,每次测试后回滚更改仍然至关重要。我已经看过了Grails 测试文档 http://grails.github.io/grails-doc/3.0.x/guide/testing.html#integrationTesting并试图使用@Rollback
如示例中所述的注释 - 它只是不起作用,更改仍然提交给数据库。
由于它是工作项目,我无法提供一些真实的代码片段,希望您理解,尽管我的测试规范看起来就像 Grails 文档示例中的那样:扩展自Specification
, @Integration
和圣杯的@Rollback
注释用于类级别。
import grails.transaction.Rollback
import grails.test.mixin.integration.Integration
@Integration
@Rollback
class SomeSpec extends Specification {
@Shared
RESTClient client
def setup() {
client = new RESTClient('http://localhost:8080')
}
我已经尝试了一切:分别对每个方法使用@Rollback,尝试使用Spring的@Rollback
同样,示例也是错误的 - 它不能应用于类,只能应用于方法,我尝试使用@Transactional
相反 - 没有成功。
另外我应该提到我正在使用的控制器和服务被注释为 Grails'@Transactional
,尽管删除所有注释并没有改变任何东西。
我用谷歌搜索了一整天,但找不到任何有用的东西,因为这些解决方案主要针对 Grails 2,所以我相信我的问题是独一无二的。其中一些解决方案建议使用 IntegrationSpec 而不是 Specific,报告它按预期工作,但它在 Grails 3 中被删除并替换为@Integration
,所以我相信这对我来说不是一个选择。
我还发现了很多@Transaction
解决方案,但这些也没有帮助,尽管我在尝试应用这些解决方案时有可能做错了什么。
因此,目前唯一的选择是尝试使测试尽可能独立于数据,并在每个测试套件之后删除并重新创建数据库,但在我看来,这是非常糟糕的解决方案。我相信没有人遇到和我一样的问题,所以我希望找到一些可以接受的解决方案。当然,我很乐意提供我忘记提及的任何缺失信息。
UPDATE: 我已经尝试过提到的解决方法here https://github.com/grails/grails-core/issues/616- 我已经使用了事务性,正如我之前所描述的,它仍然没有效果,更改仍然被提交,尽管我为每个测试收到了这对消息,这意味着它实际上正在尝试做某事。不确定它会有帮助,但它是这样的(稍作编辑):
INFO org.springframework.test.context.transaction.TransactionContext - Began transaction (1) for test context [DefaultTestContext@3dbe8e11 testClass = SomeSpec, testInstance = package.SomeSpec@5e2296ae, testMethod = $spock_feature_0_0@SomeSpec, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@6ec335b0 testClass = SomeSpec, locations = '{}', classes = '{class package.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.IntegrationTest=true}', resourceBasePath = '', contextLoader = 'grails.boot.config.GrailsApplicationContextLoader', parent = [null]]]; transaction manager [org.grails.orm.hibernate.GrailsHibernateTransactionManager@25870a3a]; rollback [true]
INFO org.springframework.test.context.transaction.TransactionContext - Rolled back transaction for test context [DefaultTestContext@3dbe8e11 testClass = SomeSpec, testInstance = package.SomeSpec@5e2296ae, testMethod = $spock_feature_0_0@SomeSpec, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@6ec335b0 testClass = SomeSpec, locations = '{}', classes = '{class package.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.IntegrationTest=true}', resourceBasePath = '', contextLoader = 'grails.boot.config.GrailsApplicationContextLoader', parent = [null]]].