本文介绍了如何将春豆注入spock测试中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的.我正在尝试针对使用JDK 1.7,Spring 3.1,Groovy 1.8.6,Spock 0.6,Maven 3.0.4的独立Java应用程序编写Spock单元测试.一个基本的hello world spock测试正在运行.但是,当我尝试测试弹簧豆时,我发现它们没有被注入.我使用的是此处所述的方法. when 块中的businessObjectDao为null.我该如何工作?

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:
            ...
    }
}

推荐答案

您很可能忘记了包含Spock Spring依赖项.

you most likely forgot to include a Spock Spring dependency.

以下是使用方法的获取方法:

Here is how to get it using:

<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'

简单的常规

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


* 0.6-groovy-1.8是当前版本,如果需要另一个版本,只需替换


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

这篇关于如何将春豆注入spock测试中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 13:04