💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# 40.3.11 使用Spock测试Spring Boot应用 如果想使用Spock测试Spring Boot应用,你需要为应用添加Spock的`spock-spring`依赖,该依赖已将Spring测试框架集成进Spock,怎么使用Spock测试Spring Boot应用取决于你使用的Spock版本。 **注** Spring Boot为Spock 1.0提供依赖管理,如果希望使用Spock 1.1,你需要覆盖`build.gradle`或`pom.xml`文件中的`spock.version`属性。 当使用Spock 1.1时,只能使用[上述注解](http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-testing-spring-boot-applications),你可以使用`@SpringBootTest`注解你的`Specification`以满足测试需求。 当使用Spock 1.0时,`@SpringBootTest`将不能用于web项目,你需要使用`@SpringApplicationConfiguration`和`@WebIntegrationTest(randomPort = true)`。 不能使用`@SpringBootTest`也就意味着你失去了自动配置的`TestRestTemplate` bean,不过可以通过以下配置创建一个等价的bean: ```java @Configuration static class TestRestTemplateConfiguration { @Bean public TestRestTemplate testRestTemplate( ObjectProvider<RestTemplateBuilder> builderProvider, Environment environment) { RestTemplateBuilder builder = builderProvider.getIfAvailable(); TestRestTemplate template = builder == null ? new TestRestTemplate() : new TestRestTemplate(builder.build()); template.setUriTemplateHandler(new LocalHostUriTemplateHandler(environment)); return template; } } ```