【问题标题】:What is the proper way to setup integration tests in grails 2.5 using junit?使用junit在grails 2.5中设置集成测试的正确方法是什么?
【发布时间】: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


    【解决方案1】:

    https://jira.grails.org/browse/GRAILS-10584

    您的测试不应扩展 GroovyTestCase,而应使用 JUnit 4 样式

    【讨论】:

    • 谢谢!我猜文档有点混乱,如果迁移涉及将所有测试转换为 JUnit 4 样式,那么对于某些项目来说可能需要大量工作。
    • 确实,很难在推进框架与支持 JUnit 4 等新框架之间取得平衡
    猜你喜欢
    • 1970-01-01
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-02
    • 2011-08-26
    相关资源
    最近更新 更多