Why is Grails ApplicationContext null in test?

709 Views Asked by At

I'm seeing the following error when running 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

This is being caused by the fact that a plugin (Rabbitmq Native Plugin: com.budjb.rabbitmq.RabbitContext.loadConfiguration) that is being used by a service that I have defined in a plugin is attempting to get config settings from grailsApplication, but this is null.

I have a plugin that defines a service that uses a bean from the Rabbitmq plugin called rabbitContext. Within my MyPluginGrailsPlugin.groovy I have:

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

I think the above should mean that the plugin loads Spring when it starts and loads the bean.

My resources.groovy in the Grails app:

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

I only have 2 test files in my app, both look like this:

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 is a Grails service I define in my plugin, and it uses the Rabbitmq plugin. I don't have any other tests in the app. What's going wrong here please?

0

There are 0 best solutions below