grails - 使用junit在grails 2.5中设置集成测试的正确方法是什么?

标签 grails junit dependency-injection integration-testing

使用 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/

相关文章:

java - Grails 映射基本集合类型

json - JSON 的 Grails 2.4 命名配置不起作用

java - Tomcat JNDI PSQLException : This connection has been closed

android - 无法解析 Android JUnit 测试运行程序

sql-server - 在 node.js 中使用带有存储库模式的 SQL Server 和 mongodb

angularjs - 如何向 Meanjs 模块注入(inject)依赖?

java - 如何验证文件是否正确上传?

java - Junit 4如何在测试套件中初始化并将其传递给测试类

unit-testing - 如何初始化一个对象并将其用于所有测试用例

python - Python 中的依赖倒置