为什么Grails ApplicationContext在测试中为null?

时间:2014-11-28 17:25:55

标签: spring grails applicationcontext

我在运行test-app时看到以下错误:

| Compiling 2 source files.
| Running 4 unit tests...
| Running 4 unit tests... 1 of 4
| Running 4 unit tests... 2 of 4
| Running 4 unit tests... 3 of 4
| Running 4 unit tests... 4 of 4
| Running 4 unit tests... 5 of 5
| Running 4 unit tests... 6 of 6
| Running 4 unit tests... 7 of 7
| Running 4 unit tests... 8 of 8
| Completed 8 unit tests, 0 failed in 0m 2s
| Error Fatal error running tests: Cannot get property 'config' on null object (NOTE: Stack trace has been filtered. Use --verbose to see entire trace.)
java.lang.NullPointerException: Cannot get property 'config' on null object
    at com.budjb.rabbitmq.RabbitContext.loadConfiguration(RabbitContext.groovy:53)
    at com.budjb.rabbitmq.RabbitContext$loadConfiguration$0.call(Unknown Source)
    at RabbitmqNativeGrailsPlugin.restartRabbitContext(RabbitmqNativeGrailsPlugin.groovy:236)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
    at RabbitmqNativeGrailsPlugin$_closure2.doCall(RabbitmqNativeGrailsPlugin.groovy:160)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
    at RabbitmqNativeGrailsPlugin$_closure2.call(RabbitmqNativeGrailsPlugin.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
| Error Fatal error running tests: Cannot get property 'config' on null object
| Tests FAILED  - view reports in /Users/john/git/daasgrails/target/test-reports
Nov 28, 2014 5:21:28 PM net.sourceforge.cobertura.coveragedata.ProjectData loadCoverageDataFromDatafile
INFO: Cobertura: Coverage data file /Users/john/git/daasgrails/cobertura.ser either does not exist or is not readable.  Creating a new data file.
Nov 28, 2014 5:21:28 PM net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler saveCoverageData
INFO: Cobertura: Saved information on 2 classes.
Done with post processing reports in 25ms
| Cobertura Code Coverage Complete (view reports in: /Users/john/git/daasgrails/target/test-reports/cobertura)
| Error Error running forked test-app: Cannot get property 'config' on null object (NOTE: Stack trace has been filtered. Use --verbose to see entire trace.)
java.lang.NullPointerException: Cannot get property 'config' on null object
    at com.budjb.rabbitmq.RabbitContext.loadConfiguration(RabbitContext.groovy:53)
    at com.budjb.rabbitmq.RabbitContext$loadConfiguration$0.call(Unknown Source)
    at RabbitmqNativeGrailsPlugin.restartRabbitContext(RabbitmqNativeGrailsPlugin.groovy:236)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
    at RabbitmqNativeGrailsPlugin$_closure2.doCall(RabbitmqNativeGrailsPlugin.groovy:160)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
    at RabbitmqNativeGrailsPlugin$_closure2.call(RabbitmqNativeGrailsPlugin.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1270)
| Error Error running forked test-app: Cannot get property 'config' on null object
| Error Forked Grails VM exited with error

这是因为我在插件中定义的服务正在使用的插件(Rabbitmq Native Plugin:com.budjb.rabbitmq.RabbitContext.loadConfiguration)试图从grailsApplication获取配置设置,但这是空的。

我有一个插件,它定义了一个使用Rabbitmq插件中名为rabbitContext的bean的服务。在我的MyPluginGrailsPlugin.groovy中,我有:

def doWithSpring = {
    // TODO Implement runtime spring config (optional)
    rabbitContext(com.budjb.rabbitmq.RabbitContext)
}

我认为上述内容应该意味着插件在启动时会加载Spring并加载bean。

Grails app中的我的resources.groovy:

beans = {
    rabbitContext(com.budjb.rabbitmq.RabbitContext) {}
}

我的应用中只有2个测试文件,两者都是这样的:

package com.example

import grails.test.mixin.TestFor
import spock.lang.Specification

/**
 * See the API for {@link grails.test.mixin.services.ServiceUnitTestMixin} for usage instructions
 */
@TestFor(AwsHealthCheckService)
class AwsHealthCheckServiceSpec extends Specification {
    static doWithSpring = {
        rabbitmqService(com.example.rabbitmq.RabbitmqService)
    }

    static doWithConfig(c) {
        c.rabbitmq = {
            connection = {
                connection host: "rabbitmq.host.com", username: "user", password: "password", virtualHost: "test"
                queues = {
                    exchange name: "logging.exchange", type: "topic", durable: true, {
                        queue name: "error.queue", binding: "error.log"
                        queue name: "logging.queue", binding: "logging.log"
                    }
                }
            }
        }
    }

    def setup() {
    }

    def cleanup() {
    }


    def "grailsApplication is not null"() {
        expect:
        grailsApplication != null
    }

    def "doWithSpring callback is executed"() {
        expect:
        grailsApplication.mainContext.getBean('rabbitmqService') != null
    }
}

AwsHealthCheckService是我在我的插件中定义的Grails服务,它使用Rabbitmq插件。我在应用中没有进行任何其他测试。请问这里出了什么问题?

0 个答案:

没有答案