且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Spring Boot 和 Spock 进行集成测试

更新时间:2022-04-10 23:58:41

问题是 Spock Spring 正在寻找 Spring 的 @ContextConfiguration 注释并且没有设法找到它.严格来说,MyTestSpec 是用 @ContextConfiguration 注释的,因为它是 @SpringApplicationConfiguration 上的元注释,但 Spock Spring 没有t 将元注释视为其搜索的一部分.有一个问题来解决这个限制.在此期间,您可以解决它.

The problem is that Spock Spring is looking for Spring's @ContextConfiguration annotation and doesn't manage to find it. Strictly speaking MyTestSpec is annotated with @ContextConfiguration as it's a meta-annotation on @SpringApplicationConfiguration but Spock Spring doesn't consider meta-annotations as part of its search. There's an issue to address this limitation. In the meantime you can work around it.

@SpringApplicationConfiguration 所做的就是使用特定于引导的上下文加载器自定义 @ContextConfiguration.这意味着您可以通过使用适当配置的 @ContextConfiguration 注释来实现相同的效果:

All that @SpringApplicationConfiguration is doing is customising @ContextConfiguration with a Boot-specific context loader. This means that you can achieve the same effect by using an appropriately configured @ContextConfiguration annotation instead:

@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyServer.class)
@WebAppConfiguration
@IntegrationTest
class MyTestSpec extends Specification {
    …
}

更新: 只是为了确保清楚(根据评论,事实并非如此),为此您需要有 org.spockframework:spock-spring 在类路径上.

Update: Just to make sure it's clear (and based on the comments, it wasn't), for this to work you need to have org.spockframework:spock-spring on the classpath.