【发布时间】:2012-04-06 08:51:07
【问题描述】:
我是 spock 的新手。我正在尝试针对使用 JDK 1.7、Spring 3.1、Groovy 1.8.6、Spock 0.6、Maven 3.0.4 的独立 Java 应用程序编写一个 spock 单元测试。一个基本的 hello world spock 测试正在运行。然而,当我尝试测试春豆时,我发现它们没有被注入。我使用提到的方法here。 when 块中的 businessObjectDao 为空。我如何让它发挥作用?
@ContextConfiguration(locations = "classpath*:test-appContext.xml")
class BusinessObjectPersistenceTest extends Specification {
@Autowired
BusinessObjectDao businessObjectDao
def "business never set at least once"() {
when:
BusinessObjectDao.getBusinessObject()
then:
...
}
}
【问题讨论】:
-
getBusinessObject方法实际上是 static 吗(BusinessObjectDao参考上的大写B)?如果是这样,那么我认为您不需要首先注入它的 instance 吗? -
可能是更新的 Spring/Spock 参考(用于注入):spockframework.org/spock/docs/2.0/module_spring.html
标签: java spring dependency-injection spock