使用 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 ) 声明如下:
Dependency Injection for Integration Tests
In order to support alternate JUnit4 test runners, Grails 2.3 no longer uses a special test runner to run tests and integration tests should no longer extend GroovyTestCase.
This change requires that any JUnit integration tests that require dependency injection now need to be annotated with:
@TestMixin(IntegrationTestMixin)
我尝试添加此注释,但没有效果:代码在
setup
中仍然失败方法。如果我注释掉 setup
并访问 deploymentService
直接在测试方法中它确实有效。所以发生了依赖注入(inject)。不只是在setup
方法。它曾经正常工作。知道如何解决这个问题吗?
谢谢
最佳答案
见 https://jira.grails.org/browse/GRAILS-10584
您的测试不应扩展 GroovyTestCase
但应该使用 JUnit 4 风格
关于grails - 使用junit在grails 2.5中设置集成测试的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602816/