【问题标题】:Does Spring 3.2 support testing with SpockSpring 3.2 是否支持使用 Spock 进行测试
【发布时间】:2013-01-21 04:05:05
【问题描述】:

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

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

我已经查看了Spring 3.2 docs under Testing

还有Spock docs under News:

但是没有什么能帮助我判断新的 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 规范测试上下文配置为与 Spring 3.2 一起使用吗?。

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

谢谢。

【问题讨论】:

  • 这个答案已经很老了。希望每个人都在使用更新版本的 Spring 和 Spock。如果您需要理由,这可能会有所帮助stackoverflow.com/a/43480494/378151

标签: spring testing spock


【解决方案1】:

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

  • 移除@RunWith注解
  • spock-spring放在测试类路径上

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

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

【讨论】:

    【解决方案2】:

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

    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 接口方法。

    测试通过。

    【讨论】:

      猜你喜欢
      • 2014-08-15
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 2016-07-16
      相关资源
      最近更新 更多