How to inject spring beans into spock test

懵懂的女人 提交于 2019-11-29 20:39:30

问题


I am new to spock. I am trying to write a spock unit test against a standalone java app that uses JDK 1.7, Spring 3.1, Groovy 1.8.6, Spock 0.6, Maven 3.0.4. A basic hello world spock test is working. However when I try to test spring beans, I find that they are not getting injected. I use the approach mentioned here. businessObjectDao is null within when block. How do I get this working?

@ContextConfiguration(locations = "classpath*:test-appContext.xml")
class BusinessObjectPersistenceTest extends Specification {
    @Autowired
    BusinessObjectDao businessObjectDao

    def "business never set at least once"() {
        when:
            BusinessObjectDao.getBusinessObject()
        then:
            ...
    }
}

回答1:


you most likely forgot to include a Spock Spring dependency.

Here is how to get it using:

maven

<dependency>
   <groupId>org.spockframework</groupId>
   <artifactId>spock-spring</artifactId>
   <version>0.6-groovy-1.8</version>
</dependency>

gradle

'org.spockframework:spock-spring:0.6-groovy-1.8'

plain groovy

@Grapes(
    @Grab(group='org.spockframework', module='spock-spring', version='0.6-groovy-1.8')
)

*0.6-groovy-1.8 is the current version, if you need another one, just substitute



来源:https://stackoverflow.com/questions/9811345/how-to-inject-spring-beans-into-spock-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!