【发布时间】:2015-06-02 17:31:59
【问题描述】:
使用 grails 2.2,以下代码(这是 GORM 类的集成测试)运行良好:
public class DbDeploymentIntegrationTests extends GroovyTestCase
{
DeploymentStorageImpl deploymentStorage
DeploymentService deploymentService
@Override
protected void setUp()
{
super.setUp()
deploymentStorage = deploymentService.deploymentService.deploymentStorage
}
...
}
当我更新到 grails 2.5 时,现在失败了:
| Failure: testIncludeDetails(org.linkedin.glu.console.domain.DbDeploymentIntegrationTests)
| java.lang.NullPointerException: Cannot get property 'deploymentService' on null object
at org.linkedin.glu.console.domain.DbDeploymentIntegrationTests.setUp(DbDeploymentIntegrationTests.groovy:41)
文档 (http://grails.github.io/grails-doc/2.5.0/guide/upgradingFrom22.html) 声明如下:
集成测试的依赖注入
为了支持 替代 JUnit4 测试运行器,Grails 2.3 不再使用特殊的 运行测试和集成测试的测试运行器不应再扩展 GroovyTestCase。
此更改要求任何需要的 JUnit 集成测试 依赖注入现在需要注释:
@TestMixin(IntegrationTestMixin)
我尝试添加此注释,但没有效果:代码在setup 方法中仍然失败。如果我注释掉setup 并直接在测试方法中访问deploymentService 它确实有效。所以发生了依赖注入。不只是在setup 方法中。而且它曾经正常工作。
知道如何解决这个问题吗?
谢谢
【问题讨论】:
标签: grails junit dependency-injection integration-testing