Grails2.4.4:服务不注入:集成测试

Grails2.4.4:服务不注入:集成测试,grails,integration-testing,spock,Grails,Integration Testing,Spock,我使用依赖性服务提供以下服务: class ParserService { def depService; private def parseLine(lineParts) { ... def set = depService.findItemByName(tmpModule.name);//depService == null ... 我尝试实施集成测试,如: @TestFor(ParserService) class ParserServiceTe

我使用依赖性服务提供以下服务:

class ParserService {
    def depService;

    private def parseLine(lineParts) {
    ...
    def set = depService.findItemByName(tmpModule.name);//depService == null
    ...
我尝试实施集成测试,如:

@TestFor(ParserService)
class ParserServiceTest extends IntegrationSpec {
    def "should not parse comment"() {
        when:
        ...
        def resultList = service.parseAnnotations(inputStream);
resources.groovy:

beans = {
}

我还有NullPointerException:depService-null

您的测试用例应该扩展类
GroovyTestCase
IntegrationSpec
,基本上就是这样。当然,依赖项注入会以通常的方式工作

class ParserServiceTest extends IntegrationSpec {
   ParserService parserService

   def "should not parse comment"() {
    when:
    ...
    def resultList = parserService.parseAnnotations(inputStream)
...

@TestFor
注释仅用于单元测试。集成测试的工作方式与普通bean类似,因此,如果您将服务定义为类中的属性(如控制器/服务/域类),那么Grails/Spring将在测试中注入依赖项。

您有一个名为DepService的服务吗?请确保你的拼写正确。我已收到通知。我可以运行应用程序(不是测试),而且一切正常。另外,请尝试发布更多代码,您如何调用服务(使用服务)