diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc index 46ee954506..e8c0a63149 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/features/testing.adoc @@ -771,7 +771,12 @@ include::{docs-java}/features/testing/springbootapplications/autoconfiguredsprin [[features.testing.spring-boot-applications.autoconfigured-webservices]] ==== Auto-configured Spring Web Services Tests -You can use `@WebServiceClientTest` to test applications that use call web services using the Spring Web Services project. + + + +[[features.testing.spring-boot-applications.autoconfigured-webservices.client]] +===== Auto-configured Spring Web Services Client Tests +You can use `@WebServiceClientTest` to test applications that call web services using the Spring Web Services project. By default, it configures a mock `WebServiceServer` bean and automatically customizes your `WebServiceTemplateBuilder`. (For more about using Web Services with Spring Boot, see "<>", earlier in this chapter.) @@ -782,7 +787,25 @@ The following example shows the `@WebServiceClientTest` annotation in use: [source,java,indent=0,subs="verbatim"] ---- -include::{docs-java}/features/testing/springbootapplications/autoconfiguredwebservices/MyWebServiceClientTests.java[] +include::{docs-java}/features/testing/springbootapplications/autoconfiguredwebservices/client/MyWebServiceClientTests.java[] +---- + + + +[[features.testing.spring-boot-applications.autoconfigured-webservices.server]] +===== Auto-configured Spring Web Services Server Tests +You can use `@WebServiceServerTest` to test applications that implement web services using the Spring Web Services project. +By default, it configures a `MockWebServiceClient` bean that can be used to call your web service endpoints. +(For more about using Web Services with Spring Boot, see "<>", earlier in this chapter.) + + +TIP: A list of the auto-configuration settings that are enabled by `@WebServiceServerTest` can be <>. + +The following example shows the `@WebServiceServerTest` annotation in use: + +[source,java,indent=0,subs="verbatim"] +---- +include::{docs-java}/features/testing/springbootapplications/autoconfiguredwebservices/server/MyWebServiceServerTests.java[] ---- diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/MyWebServiceClientTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/MyWebServiceClientTests.java similarity index 97% rename from spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/MyWebServiceClientTests.java rename to spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/MyWebServiceClientTests.java index ede71492df..41d204e11b 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/MyWebServiceClientTests.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/MyWebServiceClientTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices; +package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.client; import org.junit.jupiter.api.Test; diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/Request.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/Request.java similarity index 94% rename from spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/Request.java rename to spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/Request.java index f497c2bb71..6fa7e99808 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/Request.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/Request.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices; +package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.client; import javax.xml.bind.annotation.XmlRootElement; diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/Response.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/Response.java similarity index 95% rename from spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/Response.java rename to spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/Response.java index 4d20f7f483..4eecf9c3c6 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/Response.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/Response.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices; +package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.client; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/SomeWebService.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/SomeWebService.java similarity index 96% rename from spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/SomeWebService.java rename to spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/SomeWebService.java index fda489b253..8418964d8c 100644 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/SomeWebService.java +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/client/SomeWebService.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices; +package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.client; import org.springframework.boot.webservices.client.WebServiceTemplateBuilder; import org.springframework.stereotype.Service; diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/server/ExampleEndpoint.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/server/ExampleEndpoint.java new file mode 100644 index 0000000000..82dda7cac1 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/server/ExampleEndpoint.java @@ -0,0 +1,35 @@ +/* + * 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.features.testing.springbootapplications.autoconfiguredwebservices.server; + +import javax.xml.transform.Source; + +import org.springframework.ws.server.endpoint.annotation.Endpoint; +import org.springframework.ws.server.endpoint.annotation.PayloadRoot; +import org.springframework.ws.server.endpoint.annotation.ResponsePayload; +import org.springframework.xml.transform.StringSource; + +@Endpoint +public class ExampleEndpoint { + + @PayloadRoot(localPart = "ExampleRequest") + @ResponsePayload + public Source handleRequest() throws Exception { + return new StringSource("42"); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/server/MyWebServiceServerTests.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/server/MyWebServiceServerTests.java new file mode 100644 index 0000000000..cb2bd460ca --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/testing/springbootapplications/autoconfiguredwebservices/server/MyWebServiceServerTests.java @@ -0,0 +1,43 @@ +/* + * 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.features.testing.springbootapplications.autoconfiguredwebservices.server; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.webservices.server.WebServiceServerTest; +import org.springframework.ws.test.server.MockWebServiceClient; +import org.springframework.ws.test.server.RequestCreators; +import org.springframework.ws.test.server.ResponseMatchers; +import org.springframework.xml.transform.StringSource; + +@WebServiceServerTest(ExampleEndpoint.class) +class MyWebServiceServerTests { + + @Autowired + private MockWebServiceClient client; + + @Test + void mockServerCall() { + // @formatter:off + this.client + .sendRequest(RequestCreators.withPayload(new StringSource(""))) + .andExpect(ResponseMatchers.payload(new StringSource("42"))); + // @formatter:on + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/AutoConfigureMockWebServiceClient.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/AutoConfigureMockWebServiceClient.java new file mode 100644 index 0000000000..39254e7b57 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/AutoConfigureMockWebServiceClient.java @@ -0,0 +1,43 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.ws.test.server.MockWebServiceClient; + +/** + * Annotation that can be applied to a test class to enable auto-configuration of + * {@link MockWebServiceClient}. + * + * @author Daniil Razorenov + * @since 2.6.0 + */ +@Target({ ElementType.TYPE, ElementType.METHOD }) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ImportAutoConfiguration +public @interface AutoConfigureMockWebServiceClient { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/AutoConfigureWebServiceServer.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/AutoConfigureWebServiceServer.java new file mode 100644 index 0000000000..451ec741da --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/AutoConfigureWebServiceServer.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.test.autoconfigure.webservices.server; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; + +/** + * Annotation that can be applied to a test class to enable and configure + * auto-configuration of web service servers endpoints. + * + * @author Daniil Razorenov + * @since 2.6.0 + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@ImportAutoConfiguration +public @interface AutoConfigureWebServiceServer { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/MockWebServiceClientAutoConfiguration.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/MockWebServiceClientAutoConfiguration.java new file mode 100644 index 0000000000..bb7b1bd73f --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/MockWebServiceClientAutoConfiguration.java @@ -0,0 +1,41 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.ws.test.server.MockWebServiceClient; + +/** + * Auto-configuration for {@link MockWebServiceClient} support. + * + * @author Daniil Razorenov + * @see AutoConfigureMockWebServiceClient + * @since 2.6.0 + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnClass(MockWebServiceClient.class) +public class MockWebServiceClientAutoConfiguration { + + @Bean + MockWebServiceClient mockWebServiceClient(ApplicationContext applicationContext) { + return MockWebServiceClient.createClient(applicationContext); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTest.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTest.java new file mode 100644 index 0000000000..442a2fb167 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTest.java @@ -0,0 +1,134 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.autoconfigure.OverrideAutoConfiguration; +import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.core.annotation.AliasFor; +import org.springframework.core.env.Environment; +import org.springframework.test.context.BootstrapWith; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +/** + * Annotation that can be used for a typical Spring web service server test. Can be used + * when a test focuses only on Spring WS endpoints. + *

+ * Using this annotation will disable full auto-configuration and instead apply only + * configuration relevant to Web Service server tests (i.e. {@code Endpoint} and + * {@code EndpointInterceptor} beans but not {@code @Component}, {@code @Service} or + * {@code @Repository} beans). + *

+ * Typically {@code WebServiceServerTest} is used in combination with + * {@link org.springframework.boot.test.mock.mockito.MockBean @MockBean} or + * {@link org.springframework.context.annotation.Import @Import} to create any + * collaborators required by your {@code Endpoint} beans. + *

+ * If you are looking to load your full application configuration and use + * MockWebServiceClient, you should consider + * {@link org.springframework.boot.test.context.SpringBootTest @SpringBootTest} combined + * with {@link AutoConfigureMockWebServiceClient @AutoConfigureMockWebServiceClient} + * rather than this annotation. + * + * @author Daniil Razorenov + * @since 2.6.0 + * @see AutoConfigureMockWebServiceClient + * @see AutoConfigureWebServiceServer + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Inherited +@BootstrapWith(WebServiceServerTestContextBootstrapper.class) +@ExtendWith(SpringExtension.class) +@OverrideAutoConfiguration(enabled = false) +@TypeExcludeFilters(WebServiceServerTypeExcludeFilter.class) +@AutoConfigureWebServiceServer +@AutoConfigureMockWebServiceClient +@ImportAutoConfiguration +public @interface WebServiceServerTest { + + /** + * Properties in form {@literal key=value} that should be added to the Spring + * {@link Environment} before the test runs. + * @return the properties to add + */ + String[] properties() default {}; + + /** + * Specifies the endpoints to test. This is an alias of {@link #endpoints()} which can + * be used for brevity if no other attributes are defined. See {@link #endpoints()} + * for details. + * @return the endpoints to test + * @see #endpoints() + */ + @AliasFor("endpoints") + Class[] value() default {}; + + /** + * Specifies the endpoints to test. May be left blank if all {@code @Endpoint} beans + * should be added to the application context. + * @return the endpoints to test + * @see #value() + */ + @AliasFor("value") + Class[] endpoints() default {}; + + /** + * Determines if default filtering should be used with + * {@link SpringBootApplication @SpringBootApplication}. By default only + * {@code @Endpoint} (when no explicit {@link #endpoints() controllers} are defined) + * are included. + * @see #includeFilters() + * @see #excludeFilters() + * @return if default filters should be used + */ + boolean useDefaultFilters() default true; + + /** + * A set of include filters which can be used to add otherwise filtered beans to the + * application context. + * @return include filters to apply + */ + ComponentScan.Filter[] includeFilters() default {}; + + /** + * A set of exclude filters which can be used to filter beans that would otherwise be + * added to the application context. + * @return exclude filters to apply + */ + ComponentScan.Filter[] excludeFilters() default {}; + + /** + * Auto-configuration exclusions that should be applied for this test. + * @return auto-configuration exclusions to apply + */ + @AliasFor(annotation = ImportAutoConfiguration.class, attribute = "exclude") + Class[] excludeAutoConfiguration() default {}; + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTestContextBootstrapper.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTestContextBootstrapper.java new file mode 100644 index 0000000000..817fbd073d --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTestContextBootstrapper.java @@ -0,0 +1,46 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; +import org.springframework.core.annotation.MergedAnnotations; +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; +import org.springframework.test.context.MergedContextConfiguration; +import org.springframework.test.context.TestContextBootstrapper; +import org.springframework.test.context.web.WebMergedContextConfiguration; + +/** + * {@link TestContextBootstrapper} for {@link WebServiceServerTest @WebServiceServerTest} + * support. + * + * @author Daniil Razorenov + */ +class WebServiceServerTestContextBootstrapper extends SpringBootTestContextBootstrapper { + + @Override + protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) { + MergedContextConfiguration processedMergedConfiguration = super.processMergedContextConfiguration(mergedConfig); + return new WebMergedContextConfiguration(processedMergedConfiguration, determineResourceBasePath(mergedConfig)); + } + + @Override + protected String[] getProperties(Class testClass) { + return MergedAnnotations.from(testClass, SearchStrategy.INHERITED_ANNOTATIONS).get(WebServiceServerTest.class) + .getValue("properties", String[].class).orElse(null); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTypeExcludeFilter.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTypeExcludeFilter.java new file mode 100644 index 0000000000..1b79b6b1ff --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTypeExcludeFilter.java @@ -0,0 +1,77 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import java.util.Arrays; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; + +import org.springframework.boot.context.TypeExcludeFilter; +import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter; +import org.springframework.util.ObjectUtils; +import org.springframework.ws.server.EndpointInterceptor; +import org.springframework.ws.server.endpoint.annotation.Endpoint; + +/** + * {@link TypeExcludeFilter} for {@link WebServiceServerTest @WebServiceServerTest}. + * + * @author Daniil Razorenov + * @since 2.6.0 + */ +public class WebServiceServerTypeExcludeFilter + extends StandardAnnotationCustomizableTypeExcludeFilter { + + private static final Class[] NO_ENDPOINTS = {}; + + private static final Set> DEFAULT_INCLUDES; + + private static final Set> DEFAULT_INCLUDES_AND_ENDPOINT; + + static { + Set> includes = new LinkedHashSet<>(); + includes.add(EndpointInterceptor.class); + DEFAULT_INCLUDES = Collections.unmodifiableSet(includes); + } + + static { + Set> includes = new LinkedHashSet<>(DEFAULT_INCLUDES); + includes.add(Endpoint.class); + DEFAULT_INCLUDES_AND_ENDPOINT = Collections.unmodifiableSet(includes); + } + + private final Class[] endpoints; + + WebServiceServerTypeExcludeFilter(Class testClass) { + super(testClass); + this.endpoints = getAnnotation().getValue("endpoints", Class[].class).orElse(NO_ENDPOINTS); + } + + @Override + protected Set> getDefaultIncludes() { + if (ObjectUtils.isEmpty(this.endpoints)) { + return DEFAULT_INCLUDES_AND_ENDPOINT; + } + return DEFAULT_INCLUDES; + } + + @Override + protected Set> getComponentIncludes() { + return new LinkedHashSet<>(Arrays.asList(this.endpoints)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/package-info.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/package-info.java new file mode 100644 index 0000000000..d2e9b29a0b --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/server/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. + */ + +/** + * Auto-configuration for web service server tests. + */ +package org.springframework.boot.test.autoconfigure.webservices.server; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories index d7d3e73cac..be8d3fb3ac 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring.factories @@ -196,6 +196,14 @@ org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAuto org.springframework.boot.test.autoconfigure.webservices.client.AutoConfigureMockWebServiceServer=\ org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerAutoConfiguration +# AutoConfigureWebServiceServer auto-configuration imports +org.springframework.boot.test.autoconfigure.webservices.server.AutoConfigureWebServiceServer=\ +org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration + +# AutoConfigureMockWebServiceClient auto-configuration imports +org.springframework.boot.test.autoconfigure.webservices.server.AutoConfigureMockWebServiceClient=\ +org.springframework.boot.test.autoconfigure.webservices.server.MockWebServiceClientAutoConfiguration + # DefaultTestExecutionListenersPostProcessors org.springframework.boot.test.context.DefaultTestExecutionListenersPostProcessor=\ org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener$PostProcessor diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/ExampleWebServiceEndpoint.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/ExampleWebServiceEndpoint.java new file mode 100644 index 0000000000..84571da565 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/ExampleWebServiceEndpoint.java @@ -0,0 +1,39 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import org.springframework.ws.server.endpoint.annotation.Endpoint; +import org.springframework.ws.server.endpoint.annotation.PayloadRoot; +import org.springframework.ws.server.endpoint.annotation.RequestPayload; +import org.springframework.ws.server.endpoint.annotation.ResponsePayload; + +/** + * Example web service {@code @Endpoint} used with + * {@link WebServiceServerTest @WebServiceServerTest} tests. + * + * @author Daniil Razorenov + */ +@Endpoint +public class ExampleWebServiceEndpoint { + + @PayloadRoot(localPart = "request") + @ResponsePayload + Response payloadMethod(@RequestPayload Request request) { + return new Response(42); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/ExampleWebServiceServerApplication.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/ExampleWebServiceServerApplication.java new file mode 100644 index 0000000000..d1dfefd5a7 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/ExampleWebServiceServerApplication.java @@ -0,0 +1,30 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Example {@link SpringBootApplication @SpringBootApplication} used with + * {@link WebServiceServerTest @WebServiceServerTest} tests. + * + * @author Daniil Razorenov + */ +@SpringBootApplication +public class ExampleWebServiceServerApplication { + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/MockWebServiceClientAutoConfigurationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/MockWebServiceClientAutoConfigurationTests.java new file mode 100644 index 0000000000..ac279e87a0 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/MockWebServiceClientAutoConfigurationTests.java @@ -0,0 +1,51 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.FilteredClassLoader; +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.ws.test.server.MockWebServiceClient; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link MockWebServiceClientAutoConfiguration}. + * + * @author Daniil Razorenov + */ +class MockWebServiceClientAutoConfigurationTests { + + private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(MockWebServiceClientAutoConfiguration.class)); + + @Test + void shouldRegisterMockWebServiceClient() { + this.contextRunner.run((context) -> assertThat(context).hasSingleBean(MockWebServiceClient.class)); + } + + @Test + void shouldNotRegisterMockWebServiceClientWhenItIsNotOnTheClasspath() { + FilteredClassLoader classLoader = new FilteredClassLoader(MockWebServiceClient.class); + + this.contextRunner.withClassLoader(classLoader) + .run((context) -> assertThat(context).doesNotHaveBean(MockWebServiceClient.class)); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/Request.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/Request.java new file mode 100644 index 0000000000..c9f8ea50b3 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/Request.java @@ -0,0 +1,33 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Test request. + * + * @author Daniil Razorenov + */ +@XmlRootElement(name = "request") +class Request { + + @XmlElement(required = true) + private String message; + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/Response.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/Response.java new file mode 100644 index 0000000000..549572912f --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/Response.java @@ -0,0 +1,40 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Test response. + * + * @author Daniil Razorenov + */ +@XmlRootElement(name = "response") +class Response { + + @XmlElement(required = true) + private int code; + + Response(int code) { + this.code = code; + } + + Response() { + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerIntegrationTests.java new file mode 100644 index 0000000000..95a9683806 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerIntegrationTests.java @@ -0,0 +1,46 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ws.test.server.MockWebServiceClient; +import org.springframework.ws.test.server.RequestCreators; +import org.springframework.ws.test.server.ResponseMatchers; +import org.springframework.xml.transform.StringSource; + +/** + * Tests for {@link WebServiceServerTest @WebServiceServerTest}. + * + * @author Daniil Razorenov + */ +@WebServiceServerTest(endpoints = ExampleWebServiceEndpoint.class) +public class WebServiceServerIntegrationTests { + + @Autowired + private MockWebServiceClient mock; + + @Test + void payloadRootMethod() { + this.mock + .sendRequest( + RequestCreators.withPayload(new StringSource("Hello"))) + .andExpect(ResponseMatchers.payload(new StringSource("42"))); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerPropertiesIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerPropertiesIntegrationTests.java new file mode 100644 index 0000000000..3a0f26a5b2 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerPropertiesIntegrationTests.java @@ -0,0 +1,43 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for the {@link WebServiceServerTest#properties properties} attribute of + * {@link WebServiceServerTest @WebServiceServerTest}. + * + * @author Daniil Razorenov + */ +@WebServiceServerTest(properties = "spring.profiles.active=test") +public class WebServiceServerPropertiesIntegrationTests { + + @Autowired + private Environment environment; + + @Test + void environmentWithNewProfile() { + assertThat(this.environment.getActiveProfiles()).containsExactly("test"); + } + +} diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTypeExcludeFilterTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTypeExcludeFilterTests.java new file mode 100644 index 0000000000..8eb5c8e1a7 --- /dev/null +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/server/WebServiceServerTypeExcludeFilterTests.java @@ -0,0 +1,138 @@ +/* + * 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.test.autoconfigure.webservices.server; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +import org.springframework.context.annotation.ComponentScan.Filter; +import org.springframework.context.annotation.FilterType; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; +import org.springframework.core.type.classreading.SimpleMetadataReaderFactory; +import org.springframework.stereotype.Repository; +import org.springframework.stereotype.Service; +import org.springframework.ws.server.endpoint.annotation.Endpoint; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link WebServiceServerTypeExcludeFilter}. + * + * @author Daniil Razorenov + */ +public class WebServiceServerTypeExcludeFilterTests { + + private final MetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(); + + @Test + void matchWhenHasNoEndpoints() throws IOException { + WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(WithNoEndpoints.class); + assertThat(exclude(filter, WebService1.class)).isFalse(); + assertThat(exclude(filter, WebService2.class)).isFalse(); + assertThat(exclude(filter, ExampleService.class)).isTrue(); + assertThat(exclude(filter, ExampleRepository.class)).isTrue(); + } + + @Test + void matchWhenHasEndpoint() throws IOException { + WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(WithEndpoint.class); + assertThat(exclude(filter, WebService1.class)).isFalse(); + assertThat(exclude(filter, WebService2.class)).isTrue(); + assertThat(exclude(filter, ExampleService.class)).isTrue(); + assertThat(exclude(filter, ExampleRepository.class)).isTrue(); + } + + @Test + void matchNotUsingDefaultFilters() throws IOException { + WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(NotUsingDefaultFilters.class); + assertThat(exclude(filter, WebService1.class)).isTrue(); + assertThat(exclude(filter, WebService2.class)).isTrue(); + assertThat(exclude(filter, ExampleService.class)).isTrue(); + assertThat(exclude(filter, ExampleRepository.class)).isTrue(); + } + + @Test + void matchWithIncludeFilter() throws IOException { + WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(WithIncludeFilter.class); + assertThat(exclude(filter, WebService1.class)).isFalse(); + assertThat(exclude(filter, WebService2.class)).isFalse(); + assertThat(exclude(filter, ExampleService.class)).isTrue(); + assertThat(exclude(filter, ExampleRepository.class)).isFalse(); + } + + @Test + void matchWithExcludeFilter() throws IOException { + WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(WithExcludeFilter.class); + assertThat(exclude(filter, WebService1.class)).isTrue(); + assertThat(exclude(filter, WebService2.class)).isFalse(); + assertThat(exclude(filter, ExampleService.class)).isTrue(); + assertThat(exclude(filter, ExampleRepository.class)).isTrue(); + } + + private boolean exclude(WebServiceServerTypeExcludeFilter filter, Class type) throws IOException { + MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(type.getName()); + return filter.match(metadataReader, this.metadataReaderFactory); + } + + @WebServiceServerTest + static class WithNoEndpoints { + + } + + @WebServiceServerTest(WebService1.class) + static class WithEndpoint { + + } + + @WebServiceServerTest(useDefaultFilters = false) + static class NotUsingDefaultFilters { + + } + + @WebServiceServerTest(includeFilters = @Filter(Repository.class)) + static class WithIncludeFilter { + + } + + @WebServiceServerTest(excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = WebService1.class)) + static class WithExcludeFilter { + + } + + @Endpoint + static class WebService1 { + + } + + @Endpoint + static class WebService2 { + + } + + @Service + static class ExampleService { + + } + + @Repository + static class ExampleRepository { + + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-webservices/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-webservices/build.gradle index 8fd51c9c9d..853e4c2877 100644 --- a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-webservices/build.gradle +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-webservices/build.gradle @@ -17,4 +17,5 @@ dependencies { } testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) + testImplementation("org.springframework.ws:spring-ws-test") } diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-webservices/src/test/java/smoketest/webservices/WebServiceServerTestSampleWsApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-webservices/src/test/java/smoketest/webservices/WebServiceServerTestSampleWsApplicationTests.java new file mode 100644 index 0000000000..8b3882750e --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-webservices/src/test/java/smoketest/webservices/WebServiceServerTestSampleWsApplicationTests.java @@ -0,0 +1,70 @@ +/* + * 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 smoketest.webservices; + +import java.io.StringReader; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import javax.xml.transform.stream.StreamSource; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import smoketest.webservices.service.HumanResourceService; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.webservices.server.WebServiceServerTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; +import org.springframework.ws.test.server.MockWebServiceClient; +import org.springframework.ws.test.server.RequestCreators; +import org.springframework.ws.test.server.ResponseMatchers; + +import static org.mockito.Mockito.verify; + +/** + * Tests for {@link SampleWsApplicationTests} that use {@link WebServiceServerTest} and + * {@link MockWebServiceClient}. + * + * @author Andy Wilkinson + */ +@WebServiceServerTest +@ExtendWith(OutputCaptureExtension.class) +class WebServiceServerTestSampleWsApplicationTests { + + @MockBean + HumanResourceService service; + + @Autowired + private MockWebServiceClient client; + + @Test + void testSendingHolidayRequest(CapturedOutput output) throws ParseException { + String request = "" + + " 2013-10-20" + + " 2013-11-22 " + + " 1 John" + + " Doe "; + StreamSource source = new StreamSource(new StringReader(request)); + this.client.sendRequest(RequestCreators.withPayload(source)).andExpect(ResponseMatchers.noFault()); + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + verify(this.service).bookHoliday(dateFormat.parse("2013-10-20"), dateFormat.parse("2013-11-22"), "John Doe"); + } + +}