Inject Services in Grails Unit Test

前端 未结 2 817
北恋
北恋 2021-02-14 12:38

I know that you can simply inject a service in unit test method using:

defineBeans {
   someService(SomeService)
}

But when I need to inject se

相关标签:
2条回答
  • 2021-02-14 13:13

    you can set your member variable which is service by using ref

    MyService(MyProvider) {
            userDetailsService = ref("userDetailsService")
            springSecurityService = ref("springSecurityService")
            userService = ref("userService")
        }
    

    Hope that helps

    0 讨论(0)
  • 2021-02-14 13:20

    To use spring beans in a unit test you need to do the following:

    • Include all the services and other beans the test depends on in the defineBeans closure.
    • Set the autowire property to true for beans that need to have other beans injected.

    For example:

    defineBeans {
        someService(SomeService) { bean ->
            bean.autowire = true
        }
        some2Service(Some2Service)
    }
    
    0 讨论(0)
提交回复
热议问题