Commit 894d86de authored by Ben Ooms's avatar Ben Ooms Committed by Andy Wilkinson

Improve documentation for testing with Spock

parent 17527738
...@@ -5372,8 +5372,36 @@ on Spock's `spock-spring` module to your application's build. `spock-spring` int ...@@ -5372,8 +5372,36 @@ on Spock's `spock-spring` module to your application's build. `spock-spring` int
Spring's test framework into Spock. Spring's test framework into Spock.
NOTE: The annotations <<boot-features-testing-spring-boot-applications,described above>> NOTE: The annotations <<boot-features-testing-spring-boot-applications,described above>>
can be used with Spock, i.e. you can annotate your `Specification` with can be used with Spock 1.1, i.e. you can annotate your `Specification` with
`@SpringBootTest` to suit the needs of your tests. `@SpringBootTest` to suit the needs of your tests.
When using Spock 1.0 @SpringbootTest will not work for a web project. You need to use
'@SpringApplicationConfiguration()' and '@WebIntegrationTest(randomPort = true)'
If you want to autowire a TestRestTemplate you will have to create a bean like:
[source,java,indent=0]
----
public class TestConfiguration {
@Autowired
ApplicationContext applicationContext;
@Bean
TestRestTemplate restTemplate(){
RestTemplateBuilder builder = getRestTemplateBuilder(applicationContext);
TestRestTemplate template = new TestRestTemplate(builder.build());
template.setUriTemplateHandler(
new LocalHostUriTemplateHandler(applicationContext.getEnvironment()));
return template;
}
private RestTemplateBuilder getRestTemplateBuilder(
ApplicationContext applicationContext) {
try {
return applicationContext.getBean(RestTemplateBuilder.class);
}
catch (NoSuchBeanDefinitionException ex) {
return new RestTemplateBuilder();
}
----
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment