【问题标题】:Why is dependency injection within an integration test failing when developing my grails plugin为什么在开发我的 grails 插件时集成测试中的依赖注入失败
【发布时间】:2014-02-12 01:48:22
【问题描述】:

当使用生成的集成测试 (grails create-integration-test package.ServiceName) 时,我无法在测试中使用注入的服务 - 在 grails 插件中。

它会抛出一个非常无用的 NullPointerException

然而,同样的过程——但在 grails 应用程序中使用(grails create-app)完全没有问题。

您可以通过以下方式复制:

> grails create-plugin myServicePlugin

>grails test-app //按预期通过

>grails create-service testing.DummyService

更改\myServicePlugin\test\unit\testing\DummyServiceSpec.groovy 改变

void "test something"() {
}

void "test something"() {
    expect:
    def x = true
}

>grails test-app //通过

>grails create-integration-test testing.DummyService

更改\myServicePlugin\test\integration\testing\DummyServiceSpec.groovy 改变

void "test something"() {
}

void "test something"() {
    expect:
    def x = true
}

>grails test-app integration: //按预期通过

到目前为止一切顺利。现在编辑DummyService 来做一些事情 errmm.. Dummy-ish

更改\myServicePlugin\grails-app\services\testing\DummyService.groovy

添加

def dummySpit(){
    return true
}

def serviceMethod(){}之后

现在将服务注入到测试规范中

更改\myServicePlugin\test\integration\testing\DummyServiceSpec.groovy

添加def dummyService

下面

class DummyServiceSpec extends Specification {

并将"test something" 方法体更改为:

expect:
    assert dummyService.dummySpit()

这就是世界倒塌的地方。

>grails test-app integration:

|Loading Grails 2.3.5
|Configuring classpath
.
|Environment set to test
...............
|Compiling 1 source files
........
|Running without daemon...
.......................................
|Compiling 1 source files
.
|Running 1 integration test...
|Running 1 integration test... 1 of 1
Failure:  |
test something(testing.DummyServiceSpec)
 |
java.lang.NullPointerException
    at testing.DummyServiceSpec.test something(DummyServiceSpec.groovy:22)
|Completed 1 integration test, 1 failed in 0m 0s
.Tests FAILED 
|
 - view reports in G:\workspace\myServicePlugin\target\test-reports
Error |
Forked Grails VM exited with error

【问题讨论】:

  • 我知道这是我的第一个 SO 帖子.. 但为什么对这个问题投了反对票?我可以理解对答案投反对票……但是问题是什么?它有什么问题?
  • 通过删除琐碎的东西并强调问题的要点来编辑问题。
  • DummyServiceSpec 的第 22 行是什么?

标签: grails plugins dependency-injection integration-testing grails-2.3


【解决方案1】:

在某个阶段,create-plugin malarkey 已停止将休眠作为依赖项。

如果您将 hibernate 作为依赖项重新添加,则应该运行上述集成测试。 即。

更改\myServicePlugin\grails-app\conf\BuildConfig.groovy

runtime ":hibernate:3.6.10.7" 添加到plugins {}

注意考虑到@dmahapatro 的回答 - 最好不要导出休眠插件。因此,为避免这种情况,您可以:

test (":hibernate:3.6.10.7"){
    export = false
}

>grails test-app integration:

|Loading Grails 2.3.5
|Configuring classpath
.
|Environment set to test
........
|Installing zip hibernate-3.6.10.7.zip...
...
|Installed plugin hibernate-3.6.10.7
....................
|Compiling 1 source files
.......
|Compiling 5 source files
...........
|Running without daemon...
.............................................
|Compiling 1 source files
.
|Running 1 integration test...
|Running 1 integration test... 1 of 1
|Completed 1 integration test, 0 failed in 0m 0s
.
|Tests PASSED - view reports in G:\workspace\myServicePlugin\target\test-reports

【讨论】:

  • 你不应该那样做。
  • @dmahapatro 如果使用test 范围和export=false 包含插件,您是否赞成该解决方案?
  • 如果我是你,我不会只使用它来通过我的测试,除非有要求。 :) 这取决于您的使用情况,如果没有任何问题,您可以继续。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-10
  • 2016-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多