4

我对测试有些陌生(可怕,呵呵),所以如果这是无知的,请原谅我。

鉴于对测试框架所做的更改,spock-spring 0.7-groovy-2.0 是否与新的 Spring 3.2 版本兼容?

我查看了测试下的两个 Spring 3.2 文档

以及News 下的 Spock 文档

但是没有什么能帮助我判断新的 Spring 3.2 测试框架是否仍然允许以 Spring 3.2 测试(Spring 3.2 文档第 11.3.4 节)描述的方式配置 Spock 规范的测试上下文,以便我的注释 bean 是可注入的。

无论如何我都试过了,但是在加载测试上下文时没有成功,尽管不依赖于注入 bean 的测试通过了。

我可以提供我的@ContextConfiguration 尝试的详细信息(在上面引用的 Spring 3.2 文档第 11.3.4 节中尝试过locations=classes=模式),如果它应该工作,但现在我的问题是:Spock Specification testing context still be configured与 Spring 3.2 一起工作?

如果是这样,任何成功的例子都会很棒(没有看到任何带有 Spock 的 Spring 3.2)。

谢谢。

4

2 回答 2

3

据我所知,Spock 的 Spring 集成应该可以很好地与 Spring 3.2 中的新测试功能配合使用。与使用 JUnit 测试基于 Spring 的应用程序相比,唯一需要的更改是(一如既往):

  • 删除@RunWith注释
  • 放上spock-spring测试类路径

请注意,您不能使用扩展 Spring 测试基类的旧方法。相反,您必须使用基于注释的 Spring 测试方法。

如果您发现上述策略不起作用的情况(并且您使用 JUnit 进行了相同的测试),请在http://issues.spockframework.org提交问题。

于 2013-02-06T11:47:44.120 回答
1

在彼得回答的启发下,我发现测试上下文确实设置得很好。这是我使用的,它运行正常:

PersonServiceSpec.groovy:

@ContextConfiguration(locations="classpath*:/PersonServiceSpec-context.xml")
class PersonServiceSpec extends Specification {

    @Autowired
    PersonService personService;

    def username

    def setup() {
        this.username = "tester"
    }

    def "Does search for username pull tester" () {

        expect: "tester" == username;
    }

    def "PersonService exists" () {
        expect: personService != null;
    }

}

将此 PersonServiceSpec-context.xml 放置在类路径上(我的 Maven 项目的 src/main/resource):

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="personSevice" class="<qualifiedname>.services.TPersonServiceImpl" >
</bean>

</beans>

TPersonServiceImpl和一个实现我的PersonService接口方法的存根类。

测试通过。

于 2013-02-06T22:46:18.090 回答