Grails集成测试与多种服务

Wav*_*vyx 5 grails integration-testing unit-testing grails-2.0

测试依赖于其他服务的Grails服务的最佳做法是什么?默认的mixin TestFor正确地注入了测试中的服务,例如:

@TestFor(TopService) 
class TopServiceTests {
    @Test
    void testMethod() {
        service.method()
    }
}
Run Code Online (Sandbox Code Playgroud)

但是如果我的TopService(服务)实例依赖于另一个服务,比如InnerService:

class TopService {
    def innerService
}
Run Code Online (Sandbox Code Playgroud)

innerService将不可用,依赖注入似乎不会填充此变量.我该怎么办?

Ted*_*eid 8

集成测试不应该使用@TestFor注释,它们应该extend GroovyTestCase.测试注释仅用于单元测试(并且在集成测试中使用时会有不良行为,尤其是@Mock注释).你现在正在看到其中一种不良行为.

如果你扩展GroovyTestCase你就可以

def topService
Run Code Online (Sandbox Code Playgroud)

在测试的顶部,它将注入所有注入的依赖项.

对于单元测试用例,您只需要在setUp方法中向服务添加新的关联服务实例.就像:

@TestFor(TopService) 
class TopServiceTests {
    @Before public void setUp() {
        service.otherService = new OtherService()
    }
    ...
Run Code Online (Sandbox Code Playgroud)