Integration testing Grails services with injection

2019-05-31 02:22发布

问题:

I am having problems integration testing my Grails service because the service under test is not being injected in to my test. I have followed the advice from answers to questions else where on Stackoverflow but as yet can not get my service injected. The following class is under /<project_root>/test/integration/com/example:

package com.example

import grails.test.GrailsUnitTestCase

class MyServiceIntegrationTest extends GroovyTestCase {

    MyService service;

    public void testService() {
        assert service != null
    }
}

I have tried executing both from the command-line (grails test-app) and from within IDEA both result in the same failure, namely service is null

This is Grails 1.3.6

Any suggestions on how to get my integration test working please?

回答1:

Autowiring works the same way in integration tests as in other parts of the framework, so you need to make sure the property is named like the service except with the appropriate unCamelCase.

class MyServiceIntegrationTest extends GroovyTestCase {
    def myService

}

Assuming your service is an object named MyService.