【发布时间】:2015-04-21 12:14:27
【问题描述】:
我想集成测试一个调用使用@Transactional(propagation = Propagation.REQUIRES_NEW) 的方法的服务方法。但是,基于内部(新)事务的断言失败。
class MyService {
@Transactional(propagation = Propagation.NESTED)
def method1() {
method2()
}
@Transactional(propagation = Propagation.REQUIRES_NEW)
def method2() {
// some code that will modify the DB
}
}
class MyServiceNonTransactionalIntegrationSpec extends IntegrationSpec {
static transactional = false
def myService = new MyService()
setup() {
// populate database here
}
cleanup() {
// teardown database here after tests complete
}
def method1() {
when: "we test the main method"
myService.method1()
then: "we should be able to see the result of method 1 and method 2"
// BUT: (!!!)
// assertions based on state of database fail, perhaps because new transaction
// wrapped around method 2 has not yet committed
}
}
如何集成测试method1()?
编辑:
为了解决代理问题,我尝试了以下方法,但仍然无法正常工作:
class MyService {
def myService2
@Transactional(propagation = Propagation.NESTED)
def method1() {
myService2.method2()
}
}
class MyService2 {
@Transactional(propagation = Propagation.REQUIRES_NEW)
def method2() {
// some code that will modify the DB
}
}
class MyServiceNonTransactionalIntegrationSpec extends IntegrationSpec {
static transactional = false
def myService = new MyService()
def myService2 = new MyService2()
setup() {
myService.myService2 = myService2
// populate database here
}
// rest as before
}
【问题讨论】:
标签: spring grails transactions