Skip to content

Commit 194c9fa

Browse files
committed
Polish 'Add @WebServiceClientTest slice test support'
See gh-17274
1 parent a4104ab commit 194c9fa

File tree

7 files changed

+60
-28
lines changed

7 files changed

+60
-28
lines changed

spring-boot-project/spring-boot-docs/src/docs/asciidoc/spring-boot-features.adoc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7284,6 +7284,40 @@ include::{code-examples}/test/autoconfigure/restdocs/restassured/AdvancedConfigu
72847284

72857285

72867286

7287+
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-webservices]]
7288+
==== Auto-configured Spring Web Services Tests
7289+
You can use `@WebServiceClientTest` to test applications that use call web services using the Spring Web Services project.
7290+
By default, it configures a mock `WebServiceServer` bean and automatically customizes your `WebServiceTemplateBuilder`.
7291+
(For more about using Web Services with Spring Boot, see "<<boot-features-webservices>>", earlier in this chapter.)
7292+
7293+
7294+
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>>.
7295+
7296+
The following example shows the `@WebServiceClientTest` annotation in use:
7297+
7298+
[source,java,indent=0]
7299+
----
7300+
@WebServiceClientTest(ExampleWebServiceClient.class)
7301+
class WebServiceClientIntegrationTests {
7302+
7303+
@Autowired
7304+
private MockWebServiceServer server;
7305+
7306+
@Autowired
7307+
private ExampleWebServiceClient client;
7308+
7309+
@Test
7310+
void mockServerCall() {
7311+
this.server.expect(payload(new StringSource("<request/>"))).andRespond(
7312+
withPayload(new StringSource("<response><status>200</status></response>")));
7313+
assertThat(this.client.test()).extracting(Response::getStatus).isEqualTo(200);
7314+
}
7315+
7316+
}
7317+
----
7318+
7319+
7320+
72877321
[[boot-features-testing-spring-boot-applications-testing-auto-configured-additional-auto-config]]
72887322
==== Additional Auto-configuration and Slicing
72897323
Each slice provides one or more `@AutoConfigure...` annotations that namely defines the auto-configurations that should be included as part of a slice.

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureMockWebServiceServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
@Documented
4040
@Inherited
4141
@ImportAutoConfiguration
42-
@PropertyMapping("spring.test.webservice.client.mock-server")
42+
@PropertyMapping("spring.test.webservice.client.mockserver")
4343
public @interface AutoConfigureMockWebServiceServer {
4444

4545
/**

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @since 2.3.0
3333
*/
3434
@Configuration(proxyBeanMethods = false)
35-
@ConditionalOnProperty(prefix = "spring.test.webservice.client.mock-server", name = "enabled")
35+
@ConditionalOnProperty(prefix = "spring.test.webservice.client.mockserver", name = "enabled")
3636
@ConditionalOnClass({ MockWebServiceServer.class, WebServiceTemplate.class })
3737
public class MockWebServiceServerAutoConfiguration {
3838

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/webservices/client/MockWebServiceServerWebServiceTemplateCustomizer.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
2222
import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer;
23+
import org.springframework.util.Assert;
2324
import org.springframework.ws.client.core.WebServiceTemplate;
2425
import org.springframework.ws.test.client.MockWebServiceServer;
2526

@@ -32,7 +33,7 @@
3233
*/
3334
class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemplateCustomizer {
3435

35-
private final AtomicBoolean alreadySet = new AtomicBoolean();
36+
private final AtomicBoolean applied = new AtomicBoolean();
3637

3738
private final TestMockWebServiceServer mockServer;
3839

@@ -42,12 +43,8 @@ class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemp
4243

4344
@Override
4445
public void customize(WebServiceTemplate webServiceTemplate) {
45-
if (this.alreadySet.compareAndSet(false, true)) {
46-
webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender());
47-
}
48-
else {
49-
throw new IllegalStateException("@WebServiceClientTest supports only a single WebServiceTemplate");
50-
}
46+
Assert.state(!this.applied.getAndSet(true), "@WebServiceClientTest supports only a single WebServiceTemplate");
47+
webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender());
5148
}
5249

5350
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/AutoConfigureWebServiceClientWebServiceTemplateIntegrationTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,11 @@
2424
import org.springframework.context.annotation.Import;
2525
import org.springframework.ws.client.core.WebServiceTemplate;
2626
import org.springframework.ws.test.client.MockWebServiceServer;
27-
import org.springframework.ws.test.client.RequestMatchers;
28-
import org.springframework.ws.test.client.ResponseCreators;
2927
import org.springframework.xml.transform.StringSource;
3028

29+
import static org.springframework.ws.test.client.RequestMatchers.payload;
30+
import static org.springframework.ws.test.client.ResponseCreators.withPayload;
31+
3132
/**
3233
* Tests for {@link AutoConfigureWebServiceClient @AutoConfigureWebServiceClient} with
3334
* {@code registerWebServiceTemplate=true}.
@@ -43,12 +44,12 @@ class AutoConfigureWebServiceClientWebServiceTemplateIntegrationTests {
4344
private WebServiceTemplate webServiceTemplate;
4445

4546
@Autowired
46-
private MockWebServiceServer mockWebServiceServer;
47+
private MockWebServiceServer server;
4748

4849
@Test
4950
void webServiceTemplateTest() {
50-
this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource("<request/>")))
51-
.andRespond(ResponseCreators.withPayload(new StringSource("<response/>")));
51+
this.server.expect(payload(new StringSource("<request/>")))
52+
.andRespond(withPayload(new StringSource("<response/>")));
5253
this.webServiceTemplate.marshalSendAndReceive("https://example.com", new Request());
5354
}
5455

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientIntegrationTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.ws.client.WebServiceTransportException;
2323
import org.springframework.ws.test.client.MockWebServiceServer;
24-
import org.springframework.ws.test.client.RequestMatchers;
25-
import org.springframework.ws.test.client.ResponseCreators;
2624
import org.springframework.ws.test.support.SourceAssertionError;
2725
import org.springframework.xml.transform.StringSource;
2826

2927
import static org.assertj.core.api.Assertions.assertThat;
3028
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
29+
import static org.springframework.ws.test.client.RequestMatchers.connectionTo;
30+
import static org.springframework.ws.test.client.RequestMatchers.payload;
31+
import static org.springframework.ws.test.client.ResponseCreators.withError;
32+
import static org.springframework.ws.test.client.ResponseCreators.withPayload;
3133

3234
/**
3335
* Tests for {@link WebServiceClientTest @WebServiceClientTest}.
@@ -38,30 +40,28 @@
3840
class WebServiceClientIntegrationTests {
3941

4042
@Autowired
41-
private MockWebServiceServer mockWebServiceServer;
43+
private MockWebServiceServer server;
4244

4345
@Autowired
4446
private ExampleWebServiceClient client;
4547

4648
@Test
4749
void mockServerCall() {
48-
this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource("<request/>"))).andRespond(
49-
ResponseCreators.withPayload(new StringSource("<response><status>200</status></response>")));
50+
this.server.expect(payload(new StringSource("<request/>")))
51+
.andRespond(withPayload(new StringSource("<response><status>200</status></response>")));
5052
assertThat(this.client.test()).extracting(Response::getStatus).isEqualTo(200);
5153
}
5254

5355
@Test
5456
void mockServerCall1() {
55-
this.mockWebServiceServer.expect(RequestMatchers.connectionTo("https://example1"))
56-
.andRespond(ResponseCreators.withPayload(new StringSource("<response/>")));
57+
this.server.expect(connectionTo("https://example1")).andRespond(withPayload(new StringSource("<response/>")));
5758
assertThatExceptionOfType(SourceAssertionError.class).isThrownBy(this.client::test)
5859
.withMessageContaining("Unexpected connection expected");
5960
}
6061

6162
@Test
6263
void mockServerCall2() {
63-
this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource("<request/>")))
64-
.andRespond(ResponseCreators.withError("Invalid Request"));
64+
this.server.expect(payload(new StringSource("<request/>"))).andRespond(withError("Invalid Request"));
6565
assertThatExceptionOfType(WebServiceTransportException.class).isThrownBy(this.client::test)
6666
.withMessageContaining("Invalid Request");
6767
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/webservices/client/WebServiceClientNoComponentIntegrationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
2424
import org.springframework.context.ApplicationContext;
2525
import org.springframework.ws.test.client.MockWebServiceServer;
26-
import org.springframework.ws.test.client.RequestMatchers;
27-
import org.springframework.ws.test.client.ResponseCreators;
2826
import org.springframework.xml.transform.StringSource;
2927

3028
import static org.assertj.core.api.Assertions.assertThat;
3129
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
30+
import static org.springframework.ws.test.client.RequestMatchers.payload;
31+
import static org.springframework.ws.test.client.ResponseCreators.withPayload;
3232

3333
/**
3434
* Tests for {@link WebServiceClientTest @WebServiceClientTest} with no specific client.
@@ -45,7 +45,7 @@ class WebServiceClientNoComponentIntegrationTests {
4545
private WebServiceTemplateBuilder webServiceTemplateBuilder;
4646

4747
@Autowired
48-
private MockWebServiceServer mockWebServiceServer;
48+
private MockWebServiceServer server;
4949

5050
@Test
5151
void exampleClientIsNotInjected() {
@@ -56,8 +56,8 @@ void exampleClientIsNotInjected() {
5656
@Test
5757
void manuallyCreateBean() {
5858
ExampleWebServiceClient client = new ExampleWebServiceClient(this.webServiceTemplateBuilder);
59-
this.mockWebServiceServer.expect(RequestMatchers.payload(new StringSource("<request/>"))).andRespond(
60-
ResponseCreators.withPayload(new StringSource("<response><status>200</status></response>")));
59+
this.server.expect(payload(new StringSource("<request/>")))
60+
.andRespond(withPayload(new StringSource("<response><status>200</status></response>")));
6161
assertThat(client.test()).extracting(Response::getStatus).isEqualTo(200);
6262
}
6363

0 commit comments

Comments
 (0)