From 194c9fac64a48dd612bf17ebd052bdfd45cbde97 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 13 May 2020 21:01:36 -0700 Subject: [PATCH] Polish 'Add @WebServiceClientTest slice test support' See gh-17274 --- .../docs/asciidoc/spring-boot-features.adoc | 34 +++++++++++++++++++ .../AutoConfigureMockWebServiceServer.java | 2 +- ...MockWebServiceServerAutoConfiguration.java | 2 +- ...iceServerWebServiceTemplateCustomizer.java | 11 +++--- ...entWebServiceTemplateIntegrationTests.java | 11 +++--- .../WebServiceClientIntegrationTests.java | 18 +++++----- ...viceClientNoComponentIntegrationTests.java | 10 +++--- 7 files changed, 60 insertions(+), 28 deletions(-) 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 683f1a0b6b1..beb8d41a4e6 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 @@ -7284,6 +7284,40 @@ include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigu +[[boot-features-testing-spring-boot-applications-testing-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. +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.) + + +TIP: A list of the auto-configuration settings that are enabled by `@WebServiceClientTest` can be <>. + +The following example shows the `@WebServiceClientTest` annotation in use: + +[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); + } + + } +---- + + + [[boot-features-testing-spring-boot-applications-testing-auto-configured-additional-auto-config]] ==== Additional Auto-configuration and Slicing Each slice provides one or more `@AutoConfigure...` annotations that namely defines the auto-configurations that should be included as part of a slice. diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureMockWebServiceServer.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureMockWebServiceServer.java index 7220f6bad5d..0ab829870cb 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureMockWebServiceServer.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureMockWebServiceServer.java @@ -39,7 +39,7 @@ import org.springframework.ws.test.client.MockWebServiceServer; @Documented @Inherited @ImportAutoConfiguration -@PropertyMapping("spring.test.webservice.client.mock-server") +@PropertyMapping("spring.test.webservice.client.mockserver") public @interface AutoConfigureMockWebServiceServer { /** diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerAutoConfiguration.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerAutoConfiguration.java index f6e5f75cdb5..693d7de2bfe 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerAutoConfiguration.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerAutoConfiguration.java @@ -32,7 +32,7 @@ import org.springframework.ws.test.client.MockWebServiceServer; * @since 2.3.0 */ @Configuration(proxyBeanMethods = false) -@ConditionalOnProperty(prefix = "spring.test.webservice.client.mock-server", name = "enabled") +@ConditionalOnProperty(prefix = "spring.test.webservice.client.mockserver", name = "enabled") @ConditionalOnClass({ MockWebServiceServer.class, WebServiceTemplate.class }) public class MockWebServiceServerAutoConfiguration { diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerWebServiceTemplateCustomizer.java b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerWebServiceTemplateCustomizer.java index ddaa66ff660..1edb211887d 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerWebServiceTemplateCustomizer.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerWebServiceTemplateCustomizer.java @@ -20,6 +20,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.springframework.boot.webservices.client.WebServiceTemplateBuilder; import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer; +import org.springframework.util.Assert; import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.test.client.MockWebServiceServer; @@ -32,7 +33,7 @@ import org.springframework.ws.test.client.MockWebServiceServer; */ class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemplateCustomizer { - private final AtomicBoolean alreadySet = new AtomicBoolean(); + private final AtomicBoolean applied = new AtomicBoolean(); private final TestMockWebServiceServer mockServer; @@ -42,12 +43,8 @@ class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemp @Override public void customize(WebServiceTemplate webServiceTemplate) { - if (this.alreadySet.compareAndSet(false, true)) { - webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender()); - } - else { - throw new IllegalStateException("@WebServiceClientTest supports only a single WebServiceTemplate"); - } + Assert.state(!this.applied.getAndSet(true), "@WebServiceClientTest supports only a single WebServiceTemplate"); + webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender()); } } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureWebServiceClientWebServiceTemplateIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureWebServiceClientWebServiceTemplateIntegrationTests.java index 56093c1ed22..bd7f5c2355a 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureWebServiceClientWebServiceTemplateIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureWebServiceClientWebServiceTemplateIntegrationTests.java @@ -24,10 +24,11 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.test.client.MockWebServiceServer; -import org.springframework.ws.test.client.RequestMatchers; -import org.springframework.ws.test.client.ResponseCreators; import org.springframework.xml.transform.StringSource; +import static org.springframework.ws.test.client.RequestMatchers.payload; +import static org.springframework.ws.test.client.ResponseCreators.withPayload; + /** * Tests for {@link AutoConfigureWebServiceClient @AutoConfigureWebServiceClient} with * {@code registerWebServiceTemplate=true}. @@ -43,12 +44,12 @@ class AutoConfigureWebServiceClientWebServiceTemplateIntegrationTests { private WebServiceTemplate webServiceTemplate; @Autowired - private MockWebServiceServer mockWebServiceServer; + private MockWebServiceServer server; @Test void webServiceTemplateTest() { - this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource(""))) - .andRespond(ResponseCreators.withPayload(new StringSource(""))); + this.server.expect(payload(new StringSource(""))) + .andRespond(withPayload(new StringSource(""))); this.webServiceTemplate.marshalSendAndReceive("https://example.com", new Request()); } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientIntegrationTests.java index ef28d34e66b..2cb674e1cdc 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientIntegrationTests.java @@ -21,13 +21,15 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ws.client.WebServiceTransportException; import org.springframework.ws.test.client.MockWebServiceServer; -import org.springframework.ws.test.client.RequestMatchers; -import org.springframework.ws.test.client.ResponseCreators; import org.springframework.ws.test.support.SourceAssertionError; import org.springframework.xml.transform.StringSource; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.springframework.ws.test.client.RequestMatchers.connectionTo; +import static org.springframework.ws.test.client.RequestMatchers.payload; +import static org.springframework.ws.test.client.ResponseCreators.withError; +import static org.springframework.ws.test.client.ResponseCreators.withPayload; /** * Tests for {@link WebServiceClientTest @WebServiceClientTest}. @@ -38,30 +40,28 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; class WebServiceClientIntegrationTests { @Autowired - private MockWebServiceServer mockWebServiceServer; + private MockWebServiceServer server; @Autowired private ExampleWebServiceClient client; @Test void mockServerCall() { - this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource(""))).andRespond( - ResponseCreators.withPayload(new StringSource("200"))); + this.server.expect(payload(new StringSource(""))) + .andRespond(withPayload(new StringSource("200"))); assertThat(this.client.test()).extracting(Response::getStatus).isEqualTo(200); } @Test void mockServerCall1() { - this.mockWebServiceServer.expect(RequestMatchers.connectionTo("https://example1")) - .andRespond(ResponseCreators.withPayload(new StringSource(""))); + this.server.expect(connectionTo("https://example1")).andRespond(withPayload(new StringSource(""))); assertThatExceptionOfType(SourceAssertionError.class).isThrownBy(this.client::test) .withMessageContaining("Unexpected connection expected"); } @Test void mockServerCall2() { - this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource(""))) - .andRespond(ResponseCreators.withError("Invalid Request")); + this.server.expect(payload(new StringSource(""))).andRespond(withError("Invalid Request")); assertThatExceptionOfType(WebServiceTransportException.class).isThrownBy(this.client::test) .withMessageContaining("Invalid Request"); } diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientNoComponentIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientNoComponentIntegrationTests.java index 7d76b7d3cbd..fdf01f2a133 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientNoComponentIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientNoComponentIntegrationTests.java @@ -23,12 +23,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.webservices.client.WebServiceTemplateBuilder; import org.springframework.context.ApplicationContext; import org.springframework.ws.test.client.MockWebServiceServer; -import org.springframework.ws.test.client.RequestMatchers; -import org.springframework.ws.test.client.ResponseCreators; import org.springframework.xml.transform.StringSource; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.springframework.ws.test.client.RequestMatchers.payload; +import static org.springframework.ws.test.client.ResponseCreators.withPayload; /** * Tests for {@link WebServiceClientTest @WebServiceClientTest} with no specific client. @@ -45,7 +45,7 @@ class WebServiceClientNoComponentIntegrationTests { private WebServiceTemplateBuilder webServiceTemplateBuilder; @Autowired - private MockWebServiceServer mockWebServiceServer; + private MockWebServiceServer server; @Test void exampleClientIsNotInjected() { @@ -56,8 +56,8 @@ class WebServiceClientNoComponentIntegrationTests { @Test void manuallyCreateBean() { ExampleWebServiceClient client = new ExampleWebServiceClient(this.webServiceTemplateBuilder); - this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource(""))).andRespond( - ResponseCreators.withPayload(new StringSource("200"))); + this.server.expect(payload(new StringSource(""))) + .andRespond(withPayload(new StringSource("200"))); assertThat(client.test()).extracting(Response::getStatus).isEqualTo(200); }