运行集成测试的最佳方法是什么(例如,@IntegrationTest http://docs.spring.io/spring-boot/docs/1.1.0.BUILD-SNAPSHOT/api/org/springframework/boot/test/IntegrationTest.html)与斯波克?我想引导整个 Spring Boot 应用程序并执行一些 HTTP 调用来测试整个功能。
Spring Boot 应用程序在此工作 JUnit 测试中启动(首先运行应用程序,然后执行测试):
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyServer.class)
@WebAppConfiguration
@IntegrationTest
class MyTest {
RestTemplate template = new TestRestTemplate();
@Test
public void testDataRoutingWebSocketToHttp() {
def a = template.getForEntity("http://localhost:8080", String.class)
println a
}
}
但对于 Spock,应用程序无法启动(我怎样才能启动它?):
@SpringApplicationConfiguration(classes = MyServer.class)
@WebAppConfiguration
@IntegrationTest
class MyTestSpec extends Specification {
RestTemplate template = new TestRestTemplate();
def "Do my test"() {
setup:
def a = template.getForEntity("http://localhost:8080", String.class)
expect:
println a
}
}
当然,对于 Spock,我已在 Gradle 构建文件中指定了正确的依赖项:
...
dependencies {
testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
testCompile 'org.spockframework:spock-spring:0.7-groovy-2.0'
}
...
问题是 Spock Spring 正在寻找 Spring 的@ContextConfiguration
注释,但没能找到它。严格来讲MyTestSpec
is注释为@ContextConfiguration
因为它是一个元注释@SpringApplicationConfiguration
但 Spock Spring 并不将元注释视为其搜索的一部分。有一个issue https://code.google.com/p/spock/issues/detail?id=349来解决这个限制。与此同时,您可以解决这个问题。
所有这些@SpringApplicationConfiguration
正在做的是定制@ContextConfiguration
使用特定于引导的上下文加载器。这意味着您可以通过使用适当配置的来达到相同的效果@ContextConfiguration
改为注释:
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = MyServer.class)
@WebAppConfiguration
@IntegrationTest
class MyTestSpec extends Specification {
…
}
Update:只是为了确保它清晰(并且根据评论,事实并非如此),要使其发挥作用,您需要org.spockframework:spock-spring
在类路径上。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)