Polish 'Add @WebServiceClientTest slice test support'

See gh-17274
pull/21494/head
Phillip Webb 5 years ago
parent a4104ab096
commit 194c9fac64

@ -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 "<<boot-features-webservices>>", earlier in this chapter.)
TIP: A list of the auto-configuration settings that are enabled by `@WebServiceClientTest` can be <<appendix-test-auto-configuration.adoc#test-auto-configuration,found in the appendix>>.
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("<request/>"))).andRespond(
withPayload(new StringSource("<response><status>200</status></response>")));
assertThat(this.client.test()).extracting(Response::getStatus).isEqualTo(200);
}
}
----
[[boot-features-testing-spring-boot-applications-testing-auto-configured-additional-auto-config]] [[boot-features-testing-spring-boot-applications-testing-auto-configured-additional-auto-config]]
==== Additional Auto-configuration and Slicing ==== 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. Each slice provides one or more `@AutoConfigure...` annotations that namely defines the auto-configurations that should be included as part of a slice.

@ -39,7 +39,7 @@ import org.springframework.ws.test.client.MockWebServiceServer;
@Documented @Documented
@Inherited @Inherited
@ImportAutoConfiguration @ImportAutoConfiguration
@PropertyMapping("spring.test.webservice.client.mock-server") @PropertyMapping("spring.test.webservice.client.mockserver")
public @interface AutoConfigureMockWebServiceServer { public @interface AutoConfigureMockWebServiceServer {
/** /**

@ -32,7 +32,7 @@ import org.springframework.ws.test.client.MockWebServiceServer;
* @since 2.3.0 * @since 2.3.0
*/ */
@Configuration(proxyBeanMethods = false) @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 }) @ConditionalOnClass({ MockWebServiceServer.class, WebServiceTemplate.class })
public class MockWebServiceServerAutoConfiguration { public class MockWebServiceServerAutoConfiguration {

@ -20,6 +20,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder; import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer; import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer;
import org.springframework.util.Assert;
import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.test.client.MockWebServiceServer; import org.springframework.ws.test.client.MockWebServiceServer;
@ -32,7 +33,7 @@ import org.springframework.ws.test.client.MockWebServiceServer;
*/ */
class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemplateCustomizer { class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemplateCustomizer {
private final AtomicBoolean alreadySet = new AtomicBoolean(); private final AtomicBoolean applied = new AtomicBoolean();
private final TestMockWebServiceServer mockServer; private final TestMockWebServiceServer mockServer;
@ -42,12 +43,8 @@ class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemp
@Override @Override
public void customize(WebServiceTemplate webServiceTemplate) { public void customize(WebServiceTemplate webServiceTemplate) {
if (this.alreadySet.compareAndSet(false, true)) { Assert.state(!this.applied.getAndSet(true), "@WebServiceClientTest supports only a single WebServiceTemplate");
webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender()); webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender());
}
else {
throw new IllegalStateException("@WebServiceClientTest supports only a single WebServiceTemplate");
}
} }
} }

@ -24,10 +24,11 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.ws.client.core.WebServiceTemplate; import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.test.client.MockWebServiceServer; 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 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 * Tests for {@link AutoConfigureWebServiceClient @AutoConfigureWebServiceClient} with
* {@code registerWebServiceTemplate=true}. * {@code registerWebServiceTemplate=true}.
@ -43,12 +44,12 @@ class AutoConfigureWebServiceClientWebServiceTemplateIntegrationTests {
private WebServiceTemplate webServiceTemplate; private WebServiceTemplate webServiceTemplate;
@Autowired @Autowired
private MockWebServiceServer mockWebServiceServer; private MockWebServiceServer server;
@Test @Test
void webServiceTemplateTest() { void webServiceTemplateTest() {
this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource("<request/>"))) this.server.expect(payload(new StringSource("<request/>")))
.andRespond(ResponseCreators.withPayload(new StringSource("<response/>"))); .andRespond(withPayload(new StringSource("<response/>")));
this.webServiceTemplate.marshalSendAndReceive("https://example.com", new Request()); this.webServiceTemplate.marshalSendAndReceive("https://example.com", new Request());
} }

@ -21,13 +21,15 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.client.WebServiceTransportException; import org.springframework.ws.client.WebServiceTransportException;
import org.springframework.ws.test.client.MockWebServiceServer; 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.ws.test.support.SourceAssertionError;
import org.springframework.xml.transform.StringSource; import org.springframework.xml.transform.StringSource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; 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}. * Tests for {@link WebServiceClientTest @WebServiceClientTest}.
@ -38,30 +40,28 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
class WebServiceClientIntegrationTests { class WebServiceClientIntegrationTests {
@Autowired @Autowired
private MockWebServiceServer mockWebServiceServer; private MockWebServiceServer server;
@Autowired @Autowired
private ExampleWebServiceClient client; private ExampleWebServiceClient client;
@Test @Test
void mockServerCall() { void mockServerCall() {
this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource("<request/>"))).andRespond( this.server.expect(payload(new StringSource("<request/>")))
ResponseCreators.withPayload(new StringSource("<response><status>200</status></response>"))); .andRespond(withPayload(new StringSource("<response><status>200</status></response>")));
assertThat(this.client.test()).extracting(Response::getStatus).isEqualTo(200); assertThat(this.client.test()).extracting(Response::getStatus).isEqualTo(200);
} }
@Test @Test
void mockServerCall1() { void mockServerCall1() {
this.mockWebServiceServer.expect(RequestMatchers.connectionTo("https://example1")) this.server.expect(connectionTo("https://example1")).andRespond(withPayload(new StringSource("<response/>")));
.andRespond(ResponseCreators.withPayload(new StringSource("<response/>")));
assertThatExceptionOfType(SourceAssertionError.class).isThrownBy(this.client::test) assertThatExceptionOfType(SourceAssertionError.class).isThrownBy(this.client::test)
.withMessageContaining("Unexpected connection expected"); .withMessageContaining("Unexpected connection expected");
} }
@Test @Test
void mockServerCall2() { void mockServerCall2() {
this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource("<request/>"))) this.server.expect(payload(new StringSource("<request/>"))).andRespond(withError("Invalid Request"));
.andRespond(ResponseCreators.withError("Invalid Request"));
assertThatExceptionOfType(WebServiceTransportException.class).isThrownBy(this.client::test) assertThatExceptionOfType(WebServiceTransportException.class).isThrownBy(this.client::test)
.withMessageContaining("Invalid Request"); .withMessageContaining("Invalid Request");
} }

@ -23,12 +23,12 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder; import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.ws.test.client.MockWebServiceServer; 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 org.springframework.xml.transform.StringSource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; 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. * Tests for {@link WebServiceClientTest @WebServiceClientTest} with no specific client.
@ -45,7 +45,7 @@ class WebServiceClientNoComponentIntegrationTests {
private WebServiceTemplateBuilder webServiceTemplateBuilder; private WebServiceTemplateBuilder webServiceTemplateBuilder;
@Autowired @Autowired
private MockWebServiceServer mockWebServiceServer; private MockWebServiceServer server;
@Test @Test
void exampleClientIsNotInjected() { void exampleClientIsNotInjected() {
@ -56,8 +56,8 @@ class WebServiceClientNoComponentIntegrationTests {
@Test @Test
void manuallyCreateBean() { void manuallyCreateBean() {
ExampleWebServiceClient client = new ExampleWebServiceClient(this.webServiceTemplateBuilder); ExampleWebServiceClient client = new ExampleWebServiceClient(this.webServiceTemplateBuilder);
this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource("<request/>"))).andRespond( this.server.expect(payload(new StringSource("<request/>")))
ResponseCreators.withPayload(new StringSource("<response><status>200</status></response>"))); .andRespond(withPayload(new StringSource("<response><status>200</status></response>")));
assertThat(client.test()).extracting(Response::getStatus).isEqualTo(200); assertThat(client.test()).extracting(Response::getStatus).isEqualTo(200);
} }

Loading…
Cancel
Save