为什么在开发我的grails插件时集成测试中的依赖注入失败了

为什么在开发我的grails插件时集成测试中的依赖注入失败了,grails,plugins,dependency-injection,integration-testing,grails-2.3,Grails,Plugins,Dependency Injection,Integration Testing,Grails 2.3,当使用生成的集成测试(grails create Integration test package.ServiceName)时,我无法在测试中使用注入的服务-在grails插件中 它抛出了一个非常无用的NullPointerException 但是,在grails应用程序(grails创建应用程序)中使用的相同过程完全没有问题 您可以通过以下方式进行复制: >grails创建插件myServicePlugin >grails测试应用程序//按预期通过 >grails创建服务测试。DummySer

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

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

但是,在grails应用程序(grails创建应用程序)中使用的相同过程完全没有问题

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

>grails创建插件myServicePlugin

>grails测试应用程序
//按预期通过

>grails创建服务测试。DummyService

Alter
\myServicePlugin\test\unit\testing\dummmyServiceSpec.groovy
改变

>grails测试应用程序
//通过

>grails创建集成测试testing.DummyService

Alter
\myServicePlugin\test\integration\testing\dummmyServiceSpec.groovy
改变

>grails测试应用程序集成:
//按预期通过

到目前为止一切都很好。现在编辑
DummyService
来做点什么。。虚幻的

Alter
\myservicecplugin\grails app\services\testing\DummyService.groovy

defservicemethod(){}

现在将服务注入测试规范

Alter
\myServicePlugin\test\integration\testing\dummmyServiceSpec.groovy

添加
def dummyService

下面

并将
“测试某物”
方法主体更改为:

expect:
    assert dummyService.dummySpit()
这就是世界崩溃的地方

>grails测试应用程序集成:

|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
|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

在某个阶段,
create plugin
malarkey已经停止将hibernate作为依赖项

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

Alter
\myServicePlugin\grails app\conf\BuildConfig.groovy

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

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

test (":hibernate:3.6.10.7"){
    export = false
}
>grails测试应用程序集成:

|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
|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如果插件包含在
测试
范围和
export=false
中,您是否同意该解决方案?如果我是您,我不会仅用于通过测试,除非有要求。:)这取决于你的使用情况,如果没有任何问题,你可以继续。我知道这是我的第一篇SO帖子。。但是为什么在这个问题上投反对票呢?我能理解投票否决一个答案。。。但问题是什么?有什么问题吗?通过删除琐碎内容并强调问题的要点来编辑问题。DummyServiceSpec的第22行是什么?
expect:
    assert dummyService.dummySpit()
|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
test (":hibernate:3.6.10.7"){
    export = false
}
|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