2

使用生成的集成测试(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
4

1 回答 1

0

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

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

改变\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
于 2014-02-12T02:32:35.900 回答