From 5e8f383947cd34c1b45df63b7103ca33583a5ae6 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 26 Apr 2021 14:01:18 -0700 Subject: [PATCH] Extract code samples from docs See gh-6313 --- .../spring-boot-docs/build.gradle | 4 + .../docs/asciidoc/spring-boot-features.adoc | 244 ++++-------------- .../beanconditions/MyAutoConfiguration.java | 32 +++ .../beanconditions/SomeService.java | 21 ++ .../classconditions/MyAutoConfiguration.java | 42 +++ .../classconditions/SomeService.java | 21 ++ .../configurationkeys/AcmeProperties.java | 54 ++++ .../ConditionEvaluationReportTests.java | 38 +++ .../mvc/CustomizationConfiguration.java | 32 +++ .../mvc/ResultHandlerConfiguration.java | 32 +++ .../restdocs/mvc/UserDocumentationTests.java | 52 ++++ .../slicing/BatchConfiguration.java | 28 ++ .../slicing/ExampleJdbcTests.java | 27 ++ .../slicing/SampleApplication.java | 28 ++ .../slicing/TestWebMvcConfigurer.java | 27 ++ .../slicing/WebConfiguration.java | 33 +++ .../slicing/scan/SampleApplication.java | 28 ++ .../webservices/MyWebServiceClientTests.java | 51 ++++ .../applications/webservices/Request.java | 24 ++ .../applications/webservices/Response.java | 33 +++ .../webservices/SomeWebService.java | 36 +++ .../testing/utils/MyConfigFileTests.java | 32 +++ .../testing/utils/MyEnvironmentTests.java | 37 +++ .../utils/testresttemplate/MyTest.java | 36 +++ .../webservices/MyService.java | 47 ++++ .../MyWebServiceTemplateConfiguration.java | 42 +++ .../webservices/package-info.java | 20 ++ 27 files changed, 902 insertions(+), 199 deletions(-) create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/beanconditions/MyAutoConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/beanconditions/SomeService.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/classconditions/MyAutoConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/classconditions/SomeService.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/configurationkeys/AcmeProperties.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/ConditionEvaluationReportTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/CustomizationConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/ResultHandlerConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/UserDocumentationTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/BatchConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/ExampleJdbcTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/SampleApplication.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/TestWebMvcConfigurer.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/WebConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/scan/SampleApplication.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/MyWebServiceClientTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/Request.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/Response.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/SomeWebService.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/MyConfigFileTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/MyEnvironmentTests.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/testresttemplate/MyTest.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/MyService.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/MyWebServiceTemplateConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/package-info.java diff --git a/spring-boot-project/spring-boot-docs/build.gradle b/spring-boot-project/spring-boot-docs/build.gradle index cbd134124f..711c742a44 100644 --- a/spring-boot-project/spring-boot-docs/build.gradle +++ b/spring-boot-project/spring-boot-docs/build.gradle @@ -97,6 +97,7 @@ dependencies { implementation("org.springframework:spring-webflux") implementation("org.springframework.amqp:spring-amqp") implementation("org.springframework.amqp:spring-rabbit") + implementation("org.springframework.batch:spring-batch-core") implementation("org.springframework.data:spring-data-cassandra") implementation("org.springframework.data:spring-data-couchbase") implementation("org.springframework.data:spring-data-elasticsearch") @@ -107,11 +108,14 @@ dependencies { implementation("org.springframework.data:spring-data-r2dbc") implementation("org.springframework.kafka:spring-kafka") implementation("org.springframework.kafka:spring-kafka-test") + implementation("org.springframework.restdocs:spring-restdocs-mockmvc") implementation("org.springframework.restdocs:spring-restdocs-restassured") implementation("org.springframework.restdocs:spring-restdocs-webtestclient") implementation("org.springframework.security:spring-security-config") implementation("org.springframework.security:spring-security-oauth2-client") implementation("org.springframework.security:spring-security-web") + implementation("org.springframework.ws:spring-ws-core") + implementation("org.springframework.ws:spring-ws-test") implementation("org.junit.jupiter:junit-jupiter") implementation("org.yaml:snakeyaml") diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc index 163301597d..4fc3f79432 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc @@ -5649,7 +5649,7 @@ You can inject into your bean without needing to use any `@Qualifier`: [source,java,indent=0] ---- -include::{include-springbootfeatures}/jta/primary/MyBean.java[tags=*] +include::{include-springbootfeatures}/jta/primary/MyBean.java[tag=*] ---- In some situations, you might want to process certain JMS messages by using a non-XA `ConnectionFactory`. @@ -5659,14 +5659,14 @@ If you want to use a non-XA `ConnectionFactory`, you can the `nonXaJmsConnection [source,java,indent=0] ---- -include::{include-springbootfeatures}/jta/nonxa/MyBean.java[tags=*] +include::{include-springbootfeatures}/jta/nonxa/MyBean.java[tag=*] ---- For consistency, the `jmsConnectionFactory` bean is also provided by using the bean alias `xaJmsConnectionFactory`: [source,java,indent=0] ---- -include::{include-springbootfeatures}/jta/xa/MyBean.java[tags=*] +include::{include-springbootfeatures}/jta/xa/MyBean.java[tag=*] ---- @@ -6301,7 +6301,7 @@ For instance, the following example asserts that the actual number is a float va [source,java,indent=0] ---- -include::{include-springbootfeatures}/testing/applications/json/AssertJ.java[tags=*] +include::{include-springbootfeatures}/testing/applications/json/AssertJ.java[tag=*] ---- @@ -6652,67 +6652,25 @@ It can also be used to configure the host, scheme, and port that appears in any `@AutoConfigureRestDocs` customizes the `MockMvc` bean to use Spring REST Docs when testing Servlet-based web applications. You can inject it by using `@Autowired` and use it in your tests as you normally would when using Mock MVC and Spring REST Docs, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - import org.junit.jupiter.api.Test; - - import org.springframework.beans.factory.annotation.Autowired; - import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; - import org.springframework.http.MediaType; - import org.springframework.test.web.servlet.MockMvc; - - import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; - import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; - import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - - @WebMvcTest(UserController.class) - @AutoConfigureRestDocs - class UserDocumentationTests { - - @Autowired - private MockMvc mvc; - - @Test - void listUsers() throws Exception { - this.mvc.perform(get("/users").accept(MediaType.TEXT_PLAIN)) - .andExpect(status().isOk()) - .andDo(document("list-users")); - } - - } +include::{include-springbootfeatures}/testing/applications/restdocs/mvc/UserDocumentationTests.java[] ---- If you require more control over Spring REST Docs configuration than offered by the attributes of `@AutoConfigureRestDocs`, you can use a `RestDocsMockMvcConfigurationCustomizer` bean, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @TestConfiguration - static class CustomizationConfiguration - implements RestDocsMockMvcConfigurationCustomizer { - - @Override - public void customize(MockMvcRestDocumentationConfigurer configurer) { - configurer.snippets().withTemplateFormat(TemplateFormats.markdown()); - } - - } +include::{include-springbootfeatures}/testing/applications/restdocs/mvc/CustomizationConfiguration.java[] ---- If you want to make use of Spring REST Docs support for a parameterized output directory, you can create a `RestDocumentationResultHandler` bean. The auto-configuration calls `alwaysDo` with this result handler, thereby causing each `MockMvc` call to automatically generate the default snippets. The following example shows a `RestDocumentationResultHandler` being defined: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @TestConfiguration(proxyBeanMethods = false) - static class ResultHandlerConfiguration { - - @Bean - public RestDocumentationResultHandler restDocumentation() { - return MockMvcRestDocumentation.document("{method-name}"); - } - - } +include::{include-springbootfeatures}/testing/applications/restdocs/mvc/ResultHandlerConfiguration.java[] ---- @@ -6766,25 +6724,9 @@ TIP: A list of the auto-configuration settings that are enabled by `@WebServiceC The following example shows the `@WebServiceClientTest` annotation in use: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @WebServiceClientTest(ExampleWebServiceClient.class) - class WebServiceClientIntegrationTests { - - @Autowired - private MockWebServiceServer server; - - @Autowired - private ExampleWebServiceClient client; - - @Test - void mockServerCall() { - this.server.expect(payload(new StringSource(""))).andRespond( - withPayload(new StringSource("200"))); - assertThat(this.client.test()).extracting(Response::getStatus).isEqualTo(200); - } - - } +include::{include-springbootfeatures}/testing/applications/webservices/MyWebServiceClientTests.java[] ---- @@ -6794,13 +6736,9 @@ The following example shows the `@WebServiceClientTest` annotation in use: Each slice provides one or more `@AutoConfigure...` annotations that namely defines the auto-configurations that should be included as part of a slice. Additional auto-configurations can be added on a test-by-test basis by creating a custom `@AutoConfigure...` annotation or by adding `@ImportAutoConfiguration` to the test as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @JdbcTest - @ImportAutoConfiguration(IntegrationAutoConfiguration.class) - class ExampleJdbcTests { - - } +include::{include-springbootfeatures}/testing/applications/slicing/ExampleJdbcTests.java[] ---- NOTE: Make sure to not use the regular `@Import` annotation to import auto-configurations as they are handled in a specific way by Spring Boot. @@ -6825,21 +6763,17 @@ It then becomes important not to litter the application's main class with config Assume that you are using Spring Batch and you rely on the auto-configuration for it. You could define your `@SpringBootApplication` as follows: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @SpringBootApplication - @EnableBatchProcessing - public class SampleApplication { ... } +include::{include-springbootfeatures}/testing/applications/slicing/SampleApplication.java[] ---- Because this class is the source configuration for the test, any slice test actually tries to start Spring Batch, which is definitely not what you want to do. A recommended approach is to move that area-specific configuration to a separate `@Configuration` class at the same level as your application, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Configuration(proxyBeanMethods = false) - @EnableBatchProcessing - public class BatchConfiguration { ... } +include::{include-springbootfeatures}/testing/applications/slicing/BatchConfiguration.java[] ---- NOTE: Depending on the complexity of your application, you may either have a single `@Configuration` class for your customizations or one class per domain area. @@ -6848,38 +6782,25 @@ The latter approach lets you enable it in one of your tests, if necessary, with Test slices exclude `@Configuration` classes from scanning. For example, for a `@WebMvcTest`, the following configuration will not include the given `WebMvcConfigurer` bean in the application context loaded by the test slice: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Configuration - public class WebConfiguration { - @Bean - public WebMvcConfigurer testConfigurer() { - return new WebMvcConfigurer() { - ... - }; - } - } +include::{include-springbootfeatures}/testing/applications/slicing/WebConfiguration.java[] ---- The configuration below will, however, cause the custom `WebMvcConfigurer` to be loaded by the test slice. -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Component - public class TestWebMvcConfigurer implements WebMvcConfigurer { - ... - } +include::{include-springbootfeatures}/testing/applications/slicing/TestWebMvcConfigurer.java[] ---- Another source of confusion is classpath scanning. Assume that, while you structured your code in a sensible way, you need to scan an additional package. Your application may resemble the following code: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @SpringBootApplication - @ComponentScan({ "com.example.app", "org.acme.another" }) - public class SampleApplication { ... } +include::{include-springbootfeatures}/testing/applications/slicing/scan/SampleApplication.java[] ---- Doing so effectively overrides the default component scan directive with the side effect of scanning those two packages regardless of the slice that you chose. @@ -6909,17 +6830,16 @@ A few test utility classes that are generally useful when testing your applicati [[boot-features-configfileapplicationcontextinitializer-test-utility]] -==== ConfigFileApplicationContextInitializer -`ConfigFileApplicationContextInitializer` is an `ApplicationContextInitializer` that you can apply to your tests to load Spring Boot `application.properties` files. +==== ConfigDataApplicationContextInitializer +`ConfigDataApplicationContextInitializer` is an `ApplicationContextInitializer` that you can apply to your tests to load Spring Boot `application.properties` files. You can use it when you do not need the full set of features provided by `@SpringBootTest`, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @ContextConfiguration(classes = Config.class, - initializers = ConfigFileApplicationContextInitializer.class) +include::{include-springbootfeatures}/testing/utils/MyConfigFileTests.java[] ---- -NOTE: Using `ConfigFileApplicationContextInitializer` alone does not provide support for `@Value("${...}")` injection. +NOTE: Using `ConfigDataApplicationContextInitializer` alone does not provide support for `@Value("${...}")` injection. Its only job is to ensure that `application.properties` files are loaded into Spring's `Environment`. For `@Value` support, you need to either additionally configure a `PropertySourcesPlaceholderConfigurer` or use `@SpringBootTest`, which auto-configures one for you. @@ -6930,9 +6850,9 @@ For `@Value` support, you need to either additionally configure a `PropertySourc `TestPropertyValues` lets you quickly add properties to a `ConfigurableEnvironment` or `ConfigurableApplicationContext`. You can call it with `key=value` strings, as follows: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - TestPropertyValues.of("org=Spring", "name=Boot").applyTo(env); +include::{include-springbootfeatures}/testing/utils/MyEnvironmentTests.java[tag=*] ---- @@ -6967,20 +6887,9 @@ If you do use Apache's HTTP client, some additional test-friendly features are e `TestRestTemplate` can be instantiated directly in your integration tests, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - public class MyTest { - - private TestRestTemplate template = new TestRestTemplate(); - - @Test - public void testRequest() throws Exception { - HttpHeaders headers = this.template.getForEntity( - "https://myhost.example.com/example", String.class).getHeaders(); - assertThat(headers.getLocation()).hasHost("other.example.com"); - } - - } +include::{include-springbootfeatures}/testing/utils/testresttemplate/MyTest.java[] ---- Alternatively, if you use the `@SpringBootTest` annotation with `WebEnvironment.RANDOM_PORT` or `WebEnvironment.DEFINED_PORT`, you can inject a fully configured `TestRestTemplate` and start using it. @@ -7040,34 +6949,17 @@ It does, however, auto-configure a `WebServiceTemplateBuilder`, which can be use The following code shows a typical example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Service - public class MyService { - - private final WebServiceTemplate webServiceTemplate; - - public MyService(WebServiceTemplateBuilder webServiceTemplateBuilder) { - this.webServiceTemplate = webServiceTemplateBuilder.build(); - } - - public DetailsResp someWsCall(DetailsReq detailsReq) { - return (DetailsResp) this.webServiceTemplate.marshalSendAndReceive(detailsReq, new SoapActionCallback(ACTION)); - } - - } +include::{include-springbootfeatures}/webservices/MyService.java[] ---- By default, `WebServiceTemplateBuilder` detects a suitable HTTP-based `WebServiceMessageSender` using the available HTTP client libraries on the classpath. You can also customize read and connection timeouts as follows: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Bean - public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) { - return builder.messageSenders(new HttpWebServiceMessageSenderBuilder() - .setConnectTimeout(5000).setReadTimeout(2000).build()).build(); - } +include::{include-springbootfeatures}/webservices/MyWebServiceTemplateConfiguration.java[] ---- @@ -7150,25 +7042,9 @@ This mechanism does not apply the same way to `@Bean` methods where typically th To handle this scenario, a separate `@Configuration` class can be used to isolate the condition, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Configuration(proxyBeanMethods = false) - // Some conditions - public class MyAutoConfiguration { - - // Auto-configured beans - - @Configuration(proxyBeanMethods = false) - @ConditionalOnClass(EmbeddedAcmeService.class) - static class EmbeddedConfiguration { - - @Bean - @ConditionalOnMissingBean - public EmbeddedAcmeService embeddedAcmeService() { ... } - - } - - } +include::{include-springbootfeatures}/creatingautoconfiguration/classconditions/MyAutoConfiguration.java[] ---- TIP: If you use `@ConditionalOnClass` or `@ConditionalOnMissingClass` as a part of a meta-annotation to compose your own composed annotations, you must use `name` as referring to the class in such a case is not handled. @@ -7183,16 +7059,9 @@ The `search` attribute lets you limit the `ApplicationContext` hierarchy that sh When placed on a `@Bean` method, the target type defaults to the return type of the method, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Configuration(proxyBeanMethods = false) - public class MyAutoConfiguration { - - @Bean - @ConditionalOnMissingBean - public MyService myService() { ... } - - } +include::{include-springbootfeatures}/creatingautoconfiguration/beanconditions/MyAutoConfiguration.java[] ---- In the preceding example, the `myService` bean is going to be created if no bean of type `MyService` is already contained in the `ApplicationContext`. @@ -7278,17 +7147,9 @@ The runner can also be used to display the `ConditionEvaluationReport`. The report can be printed at `INFO` or `DEBUG` level. The following example shows how to use the `ConditionEvaluationReportLoggingListener` to print the report in auto-configuration tests. -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @Test - void autoConfigTest() { - ConditionEvaluationReportLoggingListener initializer = new ConditionEvaluationReportLoggingListener( - LogLevel.INFO); - ApplicationContextRunner contextRunner = new ApplicationContextRunner() - .withInitializer(initializer).run((context) -> { - // Do something... - }); - } +include::{include-springbootfeatures}/testing/ConditionEvaluationReportTests.java[tag=*] ---- @@ -7352,24 +7213,9 @@ As a rule of thumb, prefix all your keys with a namespace that you own (e.g. `ac Make sure that configuration keys are documented by adding field javadoc for each property, as shown in the following example: -[source,java,pending-extract=true,indent=0] +[source,java,indent=0] ---- - @ConfigurationProperties("acme") - public class AcmeProperties { - - /** - * Whether to check the location of acme resources. - */ - private boolean checkLocation = true; - - /** - * Timeout for establishing a connection to the acme server. - */ - private Duration loginTimeout = Duration.ofSeconds(3); - - // getters & setters - - } +include::{include-springbootfeatures}/creatingautoconfiguration/configurationkeys/AcmeProperties.java[] ---- NOTE: You should only use plain text with `@ConfigurationProperties` field Javadoc, since they are not processed before being added to the JSON. diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/beanconditions/MyAutoConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/beanconditions/MyAutoConfiguration.java new file mode 100644 index 0000000000..5b8c0f5f5e --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/beanconditions/MyAutoConfiguration.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.creatingautoconfiguration.beanconditions; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +public class MyAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public SomeService someService() { + return new SomeService(); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/beanconditions/SomeService.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/beanconditions/SomeService.java new file mode 100644 index 0000000000..a4d8b57c61 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/beanconditions/SomeService.java @@ -0,0 +1,21 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.creatingautoconfiguration.beanconditions; + +public class SomeService { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/classconditions/MyAutoConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/classconditions/MyAutoConfiguration.java new file mode 100644 index 0000000000..1b9f3fff75 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/classconditions/MyAutoConfiguration.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.creatingautoconfiguration.classconditions; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +// Some conditions ... +public class MyAutoConfiguration { + + // Auto-configured beans ... + + @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(SomeService.class) + public static class SomeServiceConfiguration { + + @Bean + @ConditionalOnMissingBean + public SomeService someService() { + return new SomeService(); + } + + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/classconditions/SomeService.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/classconditions/SomeService.java new file mode 100644 index 0000000000..ae9de297ae --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/classconditions/SomeService.java @@ -0,0 +1,21 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.creatingautoconfiguration.classconditions; + +class SomeService { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/configurationkeys/AcmeProperties.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/configurationkeys/AcmeProperties.java new file mode 100644 index 0000000000..1701ecd6fe --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/creatingautoconfiguration/configurationkeys/AcmeProperties.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.creatingautoconfiguration.configurationkeys; + +import java.time.Duration; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties("acme") +public class AcmeProperties { + + /** + * Whether to check the location of acme resources. + */ + private boolean checkLocation = true; + + /** + * Timeout for establishing a connection to the acme server. + */ + private Duration loginTimeout = Duration.ofSeconds(3); + + // @fold:on // getters/setters ... + public boolean isCheckLocation() { + return this.checkLocation; + } + + public void setCheckLocation(boolean checkLocation) { + this.checkLocation = checkLocation; + } + + public Duration getLoginTimeout() { + return this.loginTimeout; + } + + public void setLoginTimeout(Duration loginTimeout) { + this.loginTimeout = loginTimeout; + } + // @fold:off + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/ConditionEvaluationReportTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/ConditionEvaluationReportTests.java new file mode 100644 index 0000000000..3b012f9c53 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/ConditionEvaluationReportTests.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.testing; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener; +import org.springframework.boot.logging.LogLevel; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +public class ConditionEvaluationReportTests { + + // start::code[] + @Test + void autoConfigTest() { + ConditionEvaluationReportLoggingListener initializer = new ConditionEvaluationReportLoggingListener( + LogLevel.INFO); + new ApplicationContextRunner().withInitializer(initializer).run((context) -> { + // Test something... + }); + } + // end::code[] + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/CustomizationConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/CustomizationConfiguration.java new file mode 100644 index 0000000000..d6fa1897e2 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/CustomizationConfiguration.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.restdocs.mvc; + +import org.springframework.boot.test.autoconfigure.restdocs.RestDocsMockMvcConfigurationCustomizer; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.restdocs.mockmvc.MockMvcRestDocumentationConfigurer; +import org.springframework.restdocs.templates.TemplateFormats; + +@TestConfiguration +public class CustomizationConfiguration implements RestDocsMockMvcConfigurationCustomizer { + + @Override + public void customize(MockMvcRestDocumentationConfigurer configurer) { + configurer.snippets().withTemplateFormat(TemplateFormats.markdown()); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/ResultHandlerConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/ResultHandlerConfiguration.java new file mode 100644 index 0000000000..5f2c602249 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/ResultHandlerConfiguration.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.restdocs.mvc; + +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; +import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; + +@TestConfiguration(proxyBeanMethods = false) +public class ResultHandlerConfiguration { + + @Bean + public RestDocumentationResultHandler restDocumentation() { + return MockMvcRestDocumentation.document("{method-name}"); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/UserDocumentationTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/UserDocumentationTests.java new file mode 100644 index 0000000000..dda6081095 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/restdocs/mvc/UserDocumentationTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.restdocs.mvc; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@WebMvcTest(UserController.class) +@AutoConfigureRestDocs +class UserDocumentationTests { + + @Autowired + private MockMvc mvc; + + @Test + void listUsers() throws Exception { + // @formatter:off + this.mvc.perform(get("/users").accept(MediaType.TEXT_PLAIN)) + .andExpect(status().isOk()) + .andDo(document("list-users")); + // @formatter:on + } + +} +// @chomp:file + +class UserController { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/BatchConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/BatchConfiguration.java new file mode 100644 index 0000000000..e4f6900c7c --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/BatchConfiguration.java @@ -0,0 +1,28 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.slicing; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +@EnableBatchProcessing +public class BatchConfiguration { + + // ... + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/ExampleJdbcTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/ExampleJdbcTests.java new file mode 100644 index 0000000000..43ea8f20c3 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/ExampleJdbcTests.java @@ -0,0 +1,27 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.slicing; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration; +import org.springframework.boot.test.autoconfigure.jdbc.JdbcTest; + +@JdbcTest +@ImportAutoConfiguration(IntegrationAutoConfiguration.class) +class ExampleJdbcTests { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/SampleApplication.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/SampleApplication.java new file mode 100644 index 0000000000..4f8b954437 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/SampleApplication.java @@ -0,0 +1,28 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.slicing; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@EnableBatchProcessing +public class SampleApplication { + + // ... + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/TestWebMvcConfigurer.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/TestWebMvcConfigurer.java new file mode 100644 index 0000000000..cacabe951d --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/TestWebMvcConfigurer.java @@ -0,0 +1,27 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.slicing; + +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Component +public class TestWebMvcConfigurer implements WebMvcConfigurer { + + // ... + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/WebConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/WebConfiguration.java new file mode 100644 index 0000000000..10d71832ca --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/WebConfiguration.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.slicing; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfiguration { + + @Bean + public WebMvcConfigurer testConfigurer() { + return new WebMvcConfigurer() { + // ... + }; + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/scan/SampleApplication.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/scan/SampleApplication.java new file mode 100644 index 0000000000..0806e2a7c2 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/slicing/scan/SampleApplication.java @@ -0,0 +1,28 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.slicing.scan; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan({ "com.example.app", "org.acme.another" }) +public class SampleApplication { + + // ... + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/MyWebServiceClientTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/MyWebServiceClientTests.java new file mode 100644 index 0000000000..a96425deaa --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/MyWebServiceClientTests.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.webservices; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.webservices.client.WebServiceClientTest; +import org.springframework.ws.test.client.MockWebServiceServer; +import org.springframework.xml.transform.StringSource; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.ws.test.client.RequestMatchers.payload; +import static org.springframework.ws.test.client.ResponseCreators.withPayload; + +@WebServiceClientTest(SomeWebService.class) +class MyWebServiceClientTests { + + @Autowired + private MockWebServiceServer server; + + @Autowired + private SomeWebService someWebService; + + @Test + void mockServerCall() { + // @formatter:off + this.server + .expect(payload(new StringSource(""))) + .andRespond(withPayload(new StringSource("200"))); + assertThat(this.someWebService.test()) + .extracting(Response::getStatus) + .isEqualTo(200); + // @formatter:on + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/Request.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/Request.java new file mode 100644 index 0000000000..1bf2303b65 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/Request.java @@ -0,0 +1,24 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.webservices; + +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "request") +class Request { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/Response.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/Response.java new file mode 100644 index 0000000000..84d77ad65f --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/Response.java @@ -0,0 +1,33 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.webservices; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "response") +@XmlAccessorType(XmlAccessType.FIELD) +class Response { + + private int status; + + int getStatus() { + return this.status; + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/SomeWebService.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/SomeWebService.java new file mode 100644 index 0000000000..2559c417c0 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/applications/webservices/SomeWebService.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012-2020 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 + * + * https://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.docs.springbootfeatures.testing.applications.webservices; + +import org.springframework.boot.webservices.client.WebServiceTemplateBuilder; +import org.springframework.stereotype.Service; +import org.springframework.ws.client.core.WebServiceTemplate; + +@Service +public class SomeWebService { + + private final WebServiceTemplate webServiceTemplate; + + public SomeWebService(WebServiceTemplateBuilder builder) { + this.webServiceTemplate = builder.build(); + } + + public Response test() { + return (Response) this.webServiceTemplate.marshalSendAndReceive("https://example.com", new Request()); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/MyConfigFileTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/MyConfigFileTests.java new file mode 100644 index 0000000000..66e2cb359b --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/MyConfigFileTests.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.testing.utils; + +import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer; +import org.springframework.test.context.ContextConfiguration; + +@ContextConfiguration(classes = Config.class, initializers = ConfigDataApplicationContextInitializer.class) +class MyConfigFileTests { + + // ... + +} +// @chomp:file + +class Config { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/MyEnvironmentTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/MyEnvironmentTests.java new file mode 100644 index 0000000000..9f2e70e0a3 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/MyEnvironmentTests.java @@ -0,0 +1,37 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.testing.utils; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.mock.env.MockEnvironment; + +import static org.assertj.core.api.Assertions.assertThat; + +class MyEnvironmentTests { + + // start::code[] + @Test + void testPropertySources() { + MockEnvironment environment = new MockEnvironment(); + TestPropertyValues.of("org=Spring", "name=Boot").applyTo(environment); + assertThat(environment.getProperty("name")).isEqualTo("Boot"); + } + // end::code[] + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/testresttemplate/MyTest.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/testresttemplate/MyTest.java new file mode 100644 index 0000000000..96998d4dbc --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/testing/utils/testresttemplate/MyTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.testing.utils.testresttemplate; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.ResponseEntity; + +import static org.assertj.core.api.Assertions.assertThat; + +class MyTest { + + private TestRestTemplate template = new TestRestTemplate(); + + @Test + void testRequest() throws Exception { + ResponseEntity headers = this.template.getForEntity("https://myhost.example.com/example", String.class); + assertThat(headers.getHeaders().getLocation()).hasHost("other.example.com"); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/MyService.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/MyService.java new file mode 100644 index 0000000000..eff3634251 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/MyService.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.webservices; + +import org.springframework.boot.webservices.client.WebServiceTemplateBuilder; +import org.springframework.stereotype.Service; +import org.springframework.ws.client.core.WebServiceTemplate; +import org.springframework.ws.soap.client.core.SoapActionCallback; + +@Service +public class MyService { + + private final WebServiceTemplate webServiceTemplate; + + public MyService(WebServiceTemplateBuilder webServiceTemplateBuilder) { + this.webServiceTemplate = webServiceTemplateBuilder.build(); + } + + public SomeResponse someWsCall(SomeRequest detailsReq) { + return (SomeResponse) this.webServiceTemplate.marshalSendAndReceive(detailsReq, + new SoapActionCallback("https://ws.example.com/action")); + } + +} +// @chomp:file + +class SomeRequest { + +} + +class SomeResponse { + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/MyWebServiceTemplateConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/MyWebServiceTemplateConfiguration.java new file mode 100644 index 0000000000..1ecccada17 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/MyWebServiceTemplateConfiguration.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2021 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 + * + * https://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.docs.springbootfeatures.webservices; + +import java.time.Duration; + +import org.springframework.boot.webservices.client.HttpWebServiceMessageSenderBuilder; +import org.springframework.boot.webservices.client.WebServiceTemplateBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.ws.client.core.WebServiceTemplate; +import org.springframework.ws.transport.WebServiceMessageSender; + +@Configuration(proxyBeanMethods = false) +public class MyWebServiceTemplateConfiguration { + + @Bean + public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) { + // @formatter:off + WebServiceMessageSender sender = new HttpWebServiceMessageSenderBuilder() + .setConnectTimeout(Duration.ofSeconds(5)) + .setReadTimeout(Duration.ofSeconds(2)) + .build(); + return builder.messageSenders(sender).build(); + // @formatter:on + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/package-info.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/package-info.java new file mode 100644 index 0000000000..f1b3ec9220 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/springbootfeatures/webservices/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2021 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 + * + * https://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. + */ + +/** + * Examples for the "Spring Boot Features - Web Services" section. + */ +package org.springframework.boot.docs.springbootfeatures.webservices;