diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 48f93eb6c3..2128c5c2cd 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -150,6 +150,7 @@ 2.3.0 4.1.4 Bismuth-RC1 + 3.0.2 1.0.1 1.3.2 1.2.1 @@ -906,6 +907,11 @@ rxjava ${rxjava2.version} + + io.rest-assured + rest-assured + ${rest-assured.version} + io.searchbox jest diff --git a/spring-boot-docs/pom.xml b/spring-boot-docs/pom.xml index 4ce316868f..57d6bf846c 100644 --- a/spring-boot-docs/pom.xml +++ b/spring-boot-docs/pom.xml @@ -55,6 +55,14 @@ org.springframework.boot spring-boot-test-autoconfigure + + io.rest-assured + rest-assured + + + org.springframework.restdocs + spring-restdocs-restassured + ch.qos.logback diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index 481b17e61e..9ca8db743d 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -6279,8 +6279,20 @@ A list of the auto-configuration that is enabled by `@RestClientTest` can be [[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs]] ==== Auto-configured Spring REST Docs tests The `@AutoConfigureRestDocs` annotation can be used if you want to use Spring REST Docs -in your tests. It will automatically configure `MockMvc` to use Spring REST Docs and -remove the need for Spring REST Docs' JUnit rule. +in your tests with Mock MVC or REST Assured. It removes the need for Spring REST Docs' +JUnit rule. + +`@AutoConfigureRestDocs` can be used to override the default output directory +(`target/generated-snippets` if you are using Maven or `build/generated-snippets` if you +are using Gradle). It can also be used to configure the host, scheme, and port that will +appear in any documented URIs. + +[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-mock-mvc]] +===== Auto-configured Spring REST Docs tests with Mock MVC + +`@AutoConfigureRestDocs` customizes the `MockMvc` bean to use Spring REST Docs, Inject it +using `@Autowired` and use it in your tests as you normally would when using Mock MVC and +Spring REST Docs: [source,java,indent=0] ---- @@ -6315,11 +6327,9 @@ remove the need for Spring REST Docs' JUnit rule. } ---- -`@AutoConfigureRestDocs` can be used to override the default output directory -(`target/generated-snippets` if you are using Maven or `build/generated-snippets` if you -are using Gradle). It can also be used to configure the host, scheme, and port that will -appear in any documented URIs. If you require more control over Spring REST Docs' -configuration a `RestDocsMockMvcConfigurationCustomizer` bean can be used: +If you require more control over Spring REST Docs' configuration than offered by the +attributes of `@AutoConfigureRestDocs`, a `RestDocsMockMvcConfigurationCustomizer` bean +can be used: [source,java,indent=0] ---- @@ -6355,6 +6365,29 @@ automatically generate the default snippets: +[[boot-features-testing-spring-boot-applications-testing-autoconfigured-rest-docs-rest-assured]] +===== Auto-configured Spring REST Docs tests with REST Assured + +`@AutoConfigureRestDocs` makes a `RequestSpecification` bean, preconfigured to use Spring REST +Docs, available to your tests. Inject it using `@Autowired` and use it in your tests as you +normally would when using REST Assured and Spring REST Docs: + +[source,java,indent=0] +---- +include::{code-examples}/test/autoconfigure/restdocs/restassured/UserDocumentationTests.java[tag=source] +---- + +If you require more control over Spring REST Docs' configuration than offered by the +attributes of `@AutoConfigureRestDocs`, a `RestDocsRestAssuredConfigurationCustomizer` +bean can be used: + +[source,java,indent=0] +---- +include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigurationExample.java[tag=configuration] +---- + + + [[boot-features-testing-spring-boot-applications-with-spock]] ==== Using Spock to test Spring Boot applications If you wish to use Spock to test a Spring Boot application you should add a dependency diff --git a/spring-boot-docs/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/restassured/AdvancedConfigurationExample.java b/spring-boot-docs/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/restassured/AdvancedConfigurationExample.java new file mode 100644 index 0000000000..87d9404b27 --- /dev/null +++ b/spring-boot-docs/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/restassured/AdvancedConfigurationExample.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.restdocs.restassured; + +import org.springframework.boot.test.autoconfigure.restdocs.RestDocsRestAssuredConfigurationCustomizer; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer; +import org.springframework.restdocs.templates.TemplateFormats; + +public class AdvancedConfigurationExample { + + // tag::configuration[] + @TestConfiguration + public static class CustomizationConfiguration + implements RestDocsRestAssuredConfigurationCustomizer { + + @Override + public void customize(RestAssuredRestDocumentationConfigurer configurer) { + configurer.snippets().withTemplateFormat(TemplateFormats.markdown()); + } + + } + // end::configuration[] + +} diff --git a/spring-boot-docs/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/restassured/UserDocumentationTests.java b/spring-boot-docs/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/restassured/UserDocumentationTests.java new file mode 100644 index 0000000000..84578e1e95 --- /dev/null +++ b/spring-boot-docs/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/restassured/UserDocumentationTests.java @@ -0,0 +1,53 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.restdocs.restassured; + +// tag::source[] +import io.restassured.specification.RequestSpecification; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.junit4.SpringRunner; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@AutoConfigureRestDocs +public class UserDocumentationTests { + + @LocalServerPort + private int port; + + @Autowired + private RequestSpecification documentationSpec; + + @Test + public void listUsers() throws Exception { + given(this.documentationSpec).filter(document("list-users")).when() + .port(this.port).get("/").then().assertThat().statusCode(is(200)); + } + +} +// end::source[] diff --git a/spring-boot-parent/src/checkstyle/checkstyle.xml b/spring-boot-parent/src/checkstyle/checkstyle.xml index 8acb0a5eb8..75a2bbde52 100644 --- a/spring-boot-parent/src/checkstyle/checkstyle.xml +++ b/spring-boot-parent/src/checkstyle/checkstyle.xml @@ -70,7 +70,7 @@ + value="io.restassured.RestAssured.*, org.assertj.core.api.Assertions.*, org.junit.Assert.*, org.junit.Assume.*, org.junit.internal.matchers.ThrowableMessageMatcher.*, org.hamcrest.CoreMatchers.*, org.hamcrest.Matchers.*, org.springframework.boot.configurationprocessor.ConfigurationMetadataMatchers.*, org.springframework.boot.configurationprocessor.TestCompiler.*, org.springframework.boot.test.autoconfigure.AutoConfigurationImportedCondition.*, org.mockito.Mockito.*, org.mockito.BDDMockito.*, org.mockito.ArgumentMatchers.*, org.mockito.Matchers.*, org.springframework.restdocs.hypermedia.HypermediaDocumentation.*, org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.*, org.springframework.restdocs.operation.preprocess.Preprocessors.*, org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.*, org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.*, org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*, org.springframework.test.web.servlet.result.MockMvcResultMatchers.*, org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*, org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*, org.springframework.hateoas.mvc.ControllerLinkBuilder.linkTo, org.springframework.test.web.client.ExpectedCount.*, org.springframework.test.web.client.match.MockRestRequestMatchers.*, org.springframework.test.web.client.response.MockRestResponseCreators.*" /> diff --git a/spring-boot-test-autoconfigure/pom.xml b/spring-boot-test-autoconfigure/pom.xml index cf109e9383..6a84c62526 100644 --- a/spring-boot-test-autoconfigure/pom.xml +++ b/spring-boot-test-autoconfigure/pom.xml @@ -59,6 +59,11 @@ json-path true + + io.rest-assured + rest-assured + true + net.sourceforge.htmlunit htmlunit @@ -146,6 +151,11 @@ spring-restdocs-mockmvc true + + org.springframework.restdocs + spring-restdocs-restassured + true + org.springframework.security spring-security-config @@ -198,6 +208,11 @@ commons-pool2 test + + org.apache.tomcat.embed + tomcat-embed-core + true + org.aspectj aspectjrt diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java index e31c423751..7941b6cb1e 100644 --- a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfiguration.java @@ -16,8 +16,12 @@ package org.springframework.boot.test.autoconfigure.restdocs; +import io.restassured.builder.RequestSpecBuilder; +import io.restassured.specification.RequestSpecification; + import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; @@ -29,40 +33,79 @@ import org.springframework.restdocs.RestDocumentationContextProvider; import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer; import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; +import org.springframework.restdocs.restassured3.RestAssuredRestDocumentation; +import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring REST Docs. * * @author Andy Wilkinson + * @author Eddú Meléndez * @since 1.4.0 */ @Configuration -@ConditionalOnWebApplication(type = Type.SERVLET) @EnableConfigurationProperties +@ConditionalOnWebApplication public class RestDocsAutoConfiguration { - @Bean - @ConditionalOnMissingBean(MockMvcRestDocumentationConfigurer.class) - public MockMvcRestDocumentationConfigurer restDocsMockMvcConfigurer( - ObjectProvider configurationCustomizerProvider, - RestDocumentationContextProvider contextProvider) { - MockMvcRestDocumentationConfigurer configurer = MockMvcRestDocumentation - .documentationConfiguration(contextProvider); - RestDocsMockMvcConfigurationCustomizer configurationCustomizer = configurationCustomizerProvider - .getIfAvailable(); - if (configurationCustomizer != null) { - configurationCustomizer.customize(configurer); + @Configuration + @ConditionalOnClass(MockMvcRestDocumentation.class) + @ConditionalOnWebApplication(type = Type.SERVLET) + static class RestDocsMockMvcAutoConfiguration { + + @Bean + @ConditionalOnMissingBean(MockMvcRestDocumentationConfigurer.class) + public MockMvcRestDocumentationConfigurer restDocsMockMvcConfigurer( + ObjectProvider configurationCustomizerProvider, + RestDocumentationContextProvider contextProvider) { + MockMvcRestDocumentationConfigurer configurer = MockMvcRestDocumentation + .documentationConfiguration(contextProvider); + RestDocsMockMvcConfigurationCustomizer configurationCustomizer = configurationCustomizerProvider + .getIfAvailable(); + if (configurationCustomizer != null) { + configurationCustomizer.customize(configurer); + } + return configurer; } - return configurer; + + @Bean + @ConfigurationProperties(prefix = "spring.test.restdocs") + public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer( + MockMvcRestDocumentationConfigurer configurer, + ObjectProvider resultHandler) { + return new RestDocsMockMvcBuilderCustomizer(configurer, + resultHandler.getIfAvailable()); + } + } - @Bean - @ConfigurationProperties(prefix = "spring.test.restdocs") - public RestDocsMockMvcBuilderCustomizer restDocumentationConfigurer( - MockMvcRestDocumentationConfigurer configurer, - ObjectProvider resultHandler) { - return new RestDocsMockMvcBuilderCustomizer(configurer, - resultHandler.getIfAvailable()); + @Configuration + @ConditionalOnClass({ RequestSpecification.class, + RestAssuredRestDocumentation.class }) + static class RestDocsRestAssuredAutoConfiguration { + + @Bean + @ConditionalOnMissingBean(RequestSpecification.class) + public RequestSpecification restDocsRestAssuredConfigurer( + ObjectProvider configurationCustomizerProvider, + RestDocumentationContextProvider contextProvider) { + RestAssuredRestDocumentationConfigurer configurer = RestAssuredRestDocumentation + .documentationConfiguration(contextProvider); + RestDocsRestAssuredConfigurationCustomizer configurationCustomizer = configurationCustomizerProvider + .getIfAvailable(); + if (configurationCustomizer != null) { + configurationCustomizer.customize(configurer); + } + return new RequestSpecBuilder().addFilter(configurer).build(); + } + + @Bean + @ConfigurationProperties(prefix = "spring.test.restdocs") + public RestDocsRestAssuredBuilderCustomizer restAssuredBuilderCustomizer( + RequestSpecification configurer) { + return new RestDocsRestAssuredBuilderCustomizer(configurer); + } + } } diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java new file mode 100644 index 0000000000..54c600d3c8 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredBuilderCustomizer.java @@ -0,0 +1,77 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.restdocs; + +import io.restassured.specification.RequestSpecification; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.StringUtils; + +/** + * A customizer that configures Spring REST Docs with REST Assured. + * + * @author Eddú Meléndez + */ +class RestDocsRestAssuredBuilderCustomizer implements InitializingBean { + + private final RequestSpecification delegate; + + private String uriScheme; + + private String uriHost; + + private Integer uriPort; + + RestDocsRestAssuredBuilderCustomizer(RequestSpecification delegate) { + this.delegate = delegate; + } + + public String getUriScheme() { + return this.uriScheme; + } + + public void setUriScheme(String uriScheme) { + this.uriScheme = uriScheme; + } + + public String getUriHost() { + return this.uriHost; + } + + public void setUriHost(String uriHost) { + this.uriHost = uriHost; + } + + public Integer getUriPort() { + return this.uriPort; + } + + public void setUriPort(Integer uriPort) { + this.uriPort = uriPort; + } + + @Override + public void afterPropertiesSet() throws Exception { + if (StringUtils.hasText(this.uriScheme) && StringUtils.hasText(this.uriHost)) { + this.delegate.baseUri(this.uriScheme + "://" + this.uriHost); + } + if (this.uriPort != null) { + this.delegate.port(this.uriPort); + } + } + +} diff --git a/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredConfigurationCustomizer.java b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredConfigurationCustomizer.java new file mode 100644 index 0000000000..01aaba49df --- /dev/null +++ b/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsRestAssuredConfigurationCustomizer.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.restdocs; + +import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer; + +/** + * A customizer for {@link RestAssuredRestDocumentationConfigurer}. If a + * {@code RestDocsRestAssuredConfigurationCustomizer} bean is found in the application + * context it will be {@link #customize called} to customize the + * {@code RestAssuredRestDocumentationConfigurer} before it is applied. Intended for use + * only when the attributes on {@link AutoConfigureRestDocs} do not provide sufficient + * customization. + * + * @author Eddú Meléndez + * @since 2.0.0 + */ +@FunctionalInterface +public interface RestDocsRestAssuredConfigurationCustomizer { + + /** + * Customize the given {@code configurer}. + * @param configurer the configurer + */ + void customize(RestAssuredRestDocumentationConfigurer configurer); + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java similarity index 94% rename from spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java rename to spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java index a13c0230d9..6c34911ccd 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java @@ -41,14 +41,15 @@ import static org.springframework.restdocs.hypermedia.HypermediaDocumentation.li import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; /** - * Tests for {@link AutoConfigureRestDocs}. + * Integration tests for advanced configuration of {@link AutoConfigureRestDocs} with Mock + * MVC. * * @author Andy Wilkinson */ @RunWith(SpringRunner.class) @WebMvcTest(controllers = RestDocsTestController.class, secure = false) @AutoConfigureRestDocs -public class RestDocsAutoConfigurationAdvancedConfigurationIntegrationTests { +public class MockMvcRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests { @Before public void deleteSnippets() { diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationIntegrationTests.java similarity index 94% rename from spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationIntegrationTests.java rename to spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationIntegrationTests.java index becb943dfe..c385376012 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsAutoConfigurationIntegrationTests.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/MockMvcRestDocsAutoConfigurationIntegrationTests.java @@ -34,14 +34,14 @@ import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.docu import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; /** - * Tests for {@link RestDocsAutoConfiguration}. + * Integration tests for {@link RestDocsAutoConfiguration} with Mock MVC. * * @author Andy Wilkinson */ @RunWith(SpringRunner.class) @WebMvcTest @AutoConfigureRestDocs(uriScheme = "https", uriHost = "api.example.com", uriPort = 443) -public class RestDocsAutoConfigurationIntegrationTests { +public class MockMvcRestDocsAutoConfigurationIntegrationTests { @Before public void deleteSnippets() { diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java new file mode 100644 index 0000000000..12a940d5d3 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests.java @@ -0,0 +1,97 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.restdocs; + +import java.io.File; + +import io.restassured.specification.RequestSpecification; +import org.assertj.core.api.Condition; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.restdocs.restassured3.RestAssuredRestDocumentationConfigurer; +import org.springframework.restdocs.templates.TemplateFormats; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.FileSystemUtils; + +import static io.restassured.RestAssured.given; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; +import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document; +import static org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.modifyUris; + +/** + * Integration tests for advanced configuration of {@link AutoConfigureRestDocs} with REST + * Assured. + * + * @author Eddú Meléndez + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@AutoConfigureRestDocs +public class RestAssuredRestDocsAutoConfigurationAdvancedConfigurationIntegrationTests { + + @LocalServerPort + private int port; + + @Before + public void deleteSnippets() { + FileSystemUtils.deleteRecursively(new File("target/generated-snippets")); + } + + @Autowired + private RequestSpecification documentationSpec; + + @Test + public void snippetGeneration() throws Exception { + given(this.documentationSpec) + .filter(document("default-snippets", + preprocessRequest(modifyUris().scheme("https") + .host("api.example.com").removePort()))) + .when().port(this.port).get("/").then().assertThat().statusCode(is(200)); + File defaultSnippetsDir = new File("target/generated-snippets/default-snippets"); + assertThat(defaultSnippetsDir).exists(); + assertThat(new File(defaultSnippetsDir, "curl-request.md")) + .has(contentContaining("'https://api.example.com/'")); + assertThat(new File(defaultSnippetsDir, "http-request.md")) + .has(contentContaining("api.example.com")); + assertThat(new File(defaultSnippetsDir, "http-response.md")).isFile(); + } + + private Condition contentContaining(String toContain) { + return new ContentContainingCondition(toContain); + } + + @TestConfiguration + public static class CustomizationConfiguration + implements RestDocsRestAssuredConfigurationCustomizer { + + @Override + public void customize(RestAssuredRestDocumentationConfigurer configurer) { + configurer.snippets().withTemplateFormat(TemplateFormats.markdown()); + } + + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredRestDocsAutoConfigurationIntegrationTests.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredRestDocsAutoConfigurationIntegrationTests.java new file mode 100644 index 0000000000..bb83866447 --- /dev/null +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestAssuredRestDocsAutoConfigurationIntegrationTests.java @@ -0,0 +1,82 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.autoconfigure.restdocs; + +import java.io.File; + +import io.restassured.specification.RequestSpecification; +import org.assertj.core.api.Condition; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.util.FileSystemUtils; + +import static io.restassured.RestAssured.given; +import static org.assertj.core.api.Assertions.assertThat; +import static org.hamcrest.CoreMatchers.is; +import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; +import static org.springframework.restdocs.restassured3.RestAssuredRestDocumentation.document; +import static org.springframework.restdocs.restassured3.operation.preprocess.RestAssuredPreprocessors.modifyUris; + +/** + * Integration tests for {@link RestDocsAutoConfiguration} with REST Assured. + * + * @author Eddú Meléndez + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@AutoConfigureRestDocs +public class RestAssuredRestDocsAutoConfigurationIntegrationTests { + + @LocalServerPort + private int port; + + @Before + public void deleteSnippets() { + FileSystemUtils.deleteRecursively(new File("target/generated-snippets")); + } + + @Autowired + private RequestSpecification documentationSpec; + + @Test + public void defaultSnippetsAreWritten() throws Exception { + given(this.documentationSpec) + .filter(document("default-snippets", + preprocessRequest(modifyUris().scheme("https") + .host("api.example.com").removePort()))) + .when().port(this.port).get("/").then().assertThat().statusCode(is(200)); + File defaultSnippetsDir = new File("target/generated-snippets/default-snippets"); + assertThat(defaultSnippetsDir).exists(); + assertThat(new File(defaultSnippetsDir, "curl-request.adoc")) + .has(contentContaining("'https://api.example.com/'")); + assertThat(new File(defaultSnippetsDir, "http-request.adoc")) + .has(contentContaining("api.example.com")); + assertThat(new File(defaultSnippetsDir, "http-response.adoc")).isFile(); + } + + private Condition contentContaining(String toContain) { + return new ContentContainingCondition(toContain); + } + +} diff --git a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java index eef3c6095f..ce7d635616 100644 --- a/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java +++ b/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/restdocs/RestDocsTestApplication.java @@ -17,13 +17,14 @@ package org.springframework.boot.test.autoconfigure.restdocs; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; /** * Test application used with {@link AutoConfigureRestDocs} tests. * * @author Andy Wilkinson */ -@SpringBootApplication +@SpringBootApplication(exclude = SecurityAutoConfiguration.class) public class RestDocsTestApplication { }