Grails-在进行控制器测试时如何在控制器中实例化服务

Grails-在进行控制器测试时如何在控制器中实例化服务,grails,grails-controller,Grails,Grails Controller,我正在控制器中使用服务。我正在为控制器编写单元测试,但无法在控制器中实例化服务。它总是null 如果我在控制器测试类中使用new操作符实例化服务。服务类中的服务未实例化 如何在测试类中实例化服务?您可以让Spring为您完成 依赖于服务的控制器: // grails-app/controllers/demo/DemoController.groovy package demo class DemoController { def helperService def index

我正在控制器中使用服务。我正在为控制器编写单元测试,但无法在控制器中实例化服务。它总是
null

如果我在控制器测试类中使用
new
操作符实例化服务。服务类中的服务未实例化


如何在测试类中实例化服务?

您可以让Spring为您完成

依赖于服务的控制器:

// grails-app/controllers/demo/DemoController.groovy
package demo

class DemoController {
    def helperService

    def index() {
        def answer = helperService.theAnswer
        render "The answer is ${answer}"
    }
}
服务:

// grails-app/services/demo/HelperService.groovy
package demo

class HelperService {

    def getTheAnswer() {
        42
    }
}
// src/test/groovy/demo/DemoControllerSpec.groovy
package demo

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

@TestFor(DemoController)
class DemoControllerSpec extends Specification {

    static doWithSpring = {
        helperService HelperService
    }

    void "test service injection"() {
        when:
        controller.index()

        then:
        response.text == 'The answer is 42'
    }
}
// src/test/groovy/demo/AnotherDemoControllerSpec.groovy
package demo

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

@TestFor(DemoController)
class AnotherDemoControllerSpec extends Specification {

    static doWithSpring = {
        helperService DummyHelper
    }

    void "test service injection"() {
        when:
        controller.index()

        then:
        response.text == 'The answer is 2112'
    }
}

class DummyHelper {

    def getTheAnswer() {
        2112
    }
}
注入服务的单元测试:

// grails-app/services/demo/HelperService.groovy
package demo

class HelperService {

    def getTheAnswer() {
        42
    }
}
// src/test/groovy/demo/DemoControllerSpec.groovy
package demo

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

@TestFor(DemoController)
class DemoControllerSpec extends Specification {

    static doWithSpring = {
        helperService HelperService
    }

    void "test service injection"() {
        when:
        controller.index()

        then:
        response.text == 'The answer is 42'
    }
}
// src/test/groovy/demo/AnotherDemoControllerSpec.groovy
package demo

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

@TestFor(DemoController)
class AnotherDemoControllerSpec extends Specification {

    static doWithSpring = {
        helperService DummyHelper
    }

    void "test service injection"() {
        when:
        controller.index()

        then:
        response.text == 'The answer is 2112'
    }
}

class DummyHelper {

    def getTheAnswer() {
        2112
    }
}
注入虚假服务版本的单元测试:

// grails-app/services/demo/HelperService.groovy
package demo

class HelperService {

    def getTheAnswer() {
        42
    }
}
// src/test/groovy/demo/DemoControllerSpec.groovy
package demo

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

@TestFor(DemoController)
class DemoControllerSpec extends Specification {

    static doWithSpring = {
        helperService HelperService
    }

    void "test service injection"() {
        when:
        controller.index()

        then:
        response.text == 'The answer is 42'
    }
}
// src/test/groovy/demo/AnotherDemoControllerSpec.groovy
package demo

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

@TestFor(DemoController)
class AnotherDemoControllerSpec extends Specification {

    static doWithSpring = {
        helperService DummyHelper
    }

    void "test service injection"() {
        when:
        controller.index()

        then:
        response.text == 'The answer is 2112'
    }
}

class DummyHelper {

    def getTheAnswer() {
        2112
    }
}

如果您正在注入的服务依赖于其他bean,那么也可以在
doWithSpring
中装配这些bean。对于在服务中注入的bean,我仍然得到
null
。请稍候。我将上传一个示例。请参阅上的项目。在的提交演示了一种方法来装配一个依赖于另一个服务的服务。您可以在
doWithSpring
块中根据需要配置任意多的bean。Spring将自动连接任意深度的依赖关系树,但在单元测试中,几乎不需要装配超过2级的依赖关系。您不必为单元测试构建庞大的对象图,但您可以。