Grails Spock 单元测试需要模拟事务管理器
Grails Spock unit test requires to mock transaction manager
在 Grails 3.1.12 中,我想对服务进行单元测试:
@Transactional
class PlanService {
List<Plan> getPlans(Map params) {
def currentUser = (User)springSecurityService.getCurrentUser()
return Plan.findAllByCompany(currentUser.employer, params)
}
}
像这样:
@TestFor(PlanService)
@Mock([Plan, User, Company])
class PlanServiceSpec extends Specification {
void "Retrieve plan from the current user"() {
setup:
// create and save entities here
when: "the plans are retrieved"
def params = null
def plans = service.getPlans(params)
then: "the result should only include plans associated to the current user's company"
plans.size() == 2
}
运行 来自控制台的测试:
grails> test-app my.PlanServiceSpec -unit
失败:
my.FundingPlanServiceSpec > Retrieve plan from the current user FAILED
java.lang.IllegalStateException at PlanServiceSpec.groovy:48
并且在测试报告中(HTML):
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.
现在,如果我注释掉服务中的 @Transactional
注释,则测试通过,但这不是预期的实现。我可以通过模拟事务管理器来解决这个问题:
service.transactionManager = Mock(PlatformTransactionManager) {
getTransaction(_) >> Mock(TransactionStatus)
}
但这似乎很尴尬,如果没有错的话。
是不是有什么咒语我忘了调用?
编辑: 看起来与 an old bug 相似,但已经关闭一年多了。
您是否尝试过评论所说的解决问题的方法?如果不是,请尝试使用以下注释对测试 class 进行注释:
@TestMixin(DomainClassUnitTestMixin)
然后:
service.transactionManager = getTransactionManager()
在尝试测试事务服务时在 grails 3.3.2 中遇到同样的错误。
添加 DataTest 接口解决了我的问题。
class HelloServiceSpec extends Specification implements ServiceUnitTest<HelloService>, DataTest {
}
在 Grails 3.1.12 中,我想对服务进行单元测试:
@Transactional
class PlanService {
List<Plan> getPlans(Map params) {
def currentUser = (User)springSecurityService.getCurrentUser()
return Plan.findAllByCompany(currentUser.employer, params)
}
}
像这样:
@TestFor(PlanService)
@Mock([Plan, User, Company])
class PlanServiceSpec extends Specification {
void "Retrieve plan from the current user"() {
setup:
// create and save entities here
when: "the plans are retrieved"
def params = null
def plans = service.getPlans(params)
then: "the result should only include plans associated to the current user's company"
plans.size() == 2
}
运行 来自控制台的测试:
grails> test-app my.PlanServiceSpec -unit
失败:
my.FundingPlanServiceSpec > Retrieve plan from the current user FAILED
java.lang.IllegalStateException at PlanServiceSpec.groovy:48
并且在测试报告中(HTML):
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.
现在,如果我注释掉服务中的 @Transactional
注释,则测试通过,但这不是预期的实现。我可以通过模拟事务管理器来解决这个问题:
service.transactionManager = Mock(PlatformTransactionManager) {
getTransaction(_) >> Mock(TransactionStatus)
}
但这似乎很尴尬,如果没有错的话。
是不是有什么咒语我忘了调用?
编辑: 看起来与 an old bug 相似,但已经关闭一年多了。
您是否尝试过评论所说的解决问题的方法?如果不是,请尝试使用以下注释对测试 class 进行注释:
@TestMixin(DomainClassUnitTestMixin)
然后:
service.transactionManager = getTransactionManager()
在尝试测试事务服务时在 grails 3.3.2 中遇到同样的错误。
添加 DataTest 接口解决了我的问题。
class HelloServiceSpec extends Specification implements ServiceUnitTest<HelloService>, DataTest {
}