Skip to content

Commit a2187bb

Browse files
committed
Merge pull request #17274 from nosan
* gh-17274: Polish 'Add @WebServiceClientTest slice test support' Add @WebServiceClientTest slice test support Closes gh-17274
2 parents 9a1a2ca + 194c9fa commit a2187bb

24 files changed

+1113
-1
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/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ dependencies {
4545
optional("org.springframework.restdocs:spring-restdocs-webtestclient")
4646
optional("org.springframework.security:spring-security-config")
4747
optional("org.springframework.security:spring-security-test")
48+
optional("org.springframework.ws:spring-ws-core")
49+
optional("org.springframework.ws:spring-ws-test")
4850
optional("org.apache.tomcat.embed:tomcat-embed-core")
4951
optional("org.mongodb:mongodb-driver-reactivestreams")
5052
optional("org.mongodb:mongodb-driver-sync")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.webservices.client;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
28+
import org.springframework.ws.test.client.MockWebServiceServer;
29+
30+
/**
31+
* Annotation that can be applied to a test class to enable and configure
32+
* auto-configuration of a single {@link MockWebServiceServer}.
33+
*
34+
* @author Dmytro Nosan
35+
* @since 2.3.0
36+
*/
37+
@Target(ElementType.TYPE)
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Documented
40+
@Inherited
41+
@ImportAutoConfiguration
42+
@PropertyMapping("spring.test.webservice.client.mockserver")
43+
public @interface AutoConfigureMockWebServiceServer {
44+
45+
/**
46+
* If {@link MockWebServiceServer} bean should be registered. Defaults to
47+
* {@code true}.
48+
* @return if mock support is enabled
49+
*/
50+
boolean enabled() default true;
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.webservices.client;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Inherited;
22+
import java.lang.annotation.Retention;
23+
import java.lang.annotation.RetentionPolicy;
24+
import java.lang.annotation.Target;
25+
26+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
27+
import org.springframework.boot.test.autoconfigure.properties.PropertyMapping;
28+
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
29+
import org.springframework.ws.client.core.WebServiceTemplate;
30+
31+
/**
32+
* Annotation that can be applied to a test class to enable and configure
33+
* auto-configuration of web service clients.
34+
*
35+
* @author Dmytro Nosan
36+
* @since 2.3.0
37+
*/
38+
@Target(ElementType.TYPE)
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Documented
41+
@Inherited
42+
@ImportAutoConfiguration
43+
@PropertyMapping("spring.test.webservice.client")
44+
public @interface AutoConfigureWebServiceClient {
45+
46+
/**
47+
* If a {@link WebServiceTemplate} bean should be registered. Defaults to
48+
* {@code false} with the assumption that the {@link WebServiceTemplateBuilder} will
49+
* be used.
50+
* @return if a {@link WebServiceTemplate} bean should be added.
51+
*/
52+
boolean registerWebServiceTemplate() default false;
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.webservices.client;
18+
19+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.ws.client.core.WebServiceTemplate;
24+
import org.springframework.ws.test.client.MockWebServiceMessageSender;
25+
import org.springframework.ws.test.client.MockWebServiceServer;
26+
27+
/**
28+
* Auto-configuration for {@link MockWebServiceServer} support.
29+
*
30+
* @author Dmytro Nosan
31+
* @see AutoConfigureMockWebServiceServer
32+
* @since 2.3.0
33+
*/
34+
@Configuration(proxyBeanMethods = false)
35+
@ConditionalOnProperty(prefix = "spring.test.webservice.client.mockserver", name = "enabled")
36+
@ConditionalOnClass({ MockWebServiceServer.class, WebServiceTemplate.class })
37+
public class MockWebServiceServerAutoConfiguration {
38+
39+
@Bean
40+
public TestMockWebServiceServer mockWebServiceServer() {
41+
return new TestMockWebServiceServer(new MockWebServiceMessageSender());
42+
}
43+
44+
@Bean
45+
public MockWebServiceServerWebServiceTemplateCustomizer mockWebServiceServerWebServiceTemplateCustomizer(
46+
TestMockWebServiceServer mockWebServiceServer) {
47+
return new MockWebServiceServerWebServiceTemplateCustomizer(mockWebServiceServer);
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.webservices.client;
18+
19+
import org.springframework.context.ApplicationContext;
20+
import org.springframework.core.Ordered;
21+
import org.springframework.test.context.TestContext;
22+
import org.springframework.test.context.TestExecutionListener;
23+
import org.springframework.test.context.support.AbstractTestExecutionListener;
24+
import org.springframework.util.ClassUtils;
25+
import org.springframework.ws.test.client.MockWebServiceServer;
26+
27+
/**
28+
* {@link TestExecutionListener} to {@code verify} and {@code reset}
29+
* {@link MockWebServiceServer}.
30+
*
31+
* @author Dmytro Nosan
32+
* @since 2.3.0
33+
*/
34+
public class MockWebServiceServerTestExecutionListener extends AbstractTestExecutionListener {
35+
36+
private static final String MOCK_SERVER_CLASS = "org.springframework.ws.test.client.MockWebServiceServer";
37+
38+
@Override
39+
public int getOrder() {
40+
return Ordered.LOWEST_PRECEDENCE - 100;
41+
}
42+
43+
@Override
44+
public void afterTestMethod(TestContext testContext) {
45+
if (isMockWebServiceServerPresent()) {
46+
ApplicationContext applicationContext = testContext.getApplicationContext();
47+
String[] names = applicationContext.getBeanNamesForType(MockWebServiceServer.class, false, false);
48+
for (String name : names) {
49+
MockWebServiceServer mockServer = applicationContext.getBean(name, MockWebServiceServer.class);
50+
mockServer.verify();
51+
mockServer.reset();
52+
}
53+
}
54+
}
55+
56+
private boolean isMockWebServiceServerPresent() {
57+
return ClassUtils.isPresent(MOCK_SERVER_CLASS, getClass().getClassLoader());
58+
}
59+
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.webservices.client;
18+
19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
21+
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
22+
import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer;
23+
import org.springframework.util.Assert;
24+
import org.springframework.ws.client.core.WebServiceTemplate;
25+
import org.springframework.ws.test.client.MockWebServiceServer;
26+
27+
/**
28+
* {@link WebServiceTemplateCustomizer} that can be applied to a
29+
* {@link WebServiceTemplateBuilder} instances to add {@link MockWebServiceServer}
30+
* support.
31+
*
32+
* @author Dmytro Nosan
33+
*/
34+
class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemplateCustomizer {
35+
36+
private final AtomicBoolean applied = new AtomicBoolean();
37+
38+
private final TestMockWebServiceServer mockServer;
39+
40+
MockWebServiceServerWebServiceTemplateCustomizer(TestMockWebServiceServer mockServer) {
41+
this.mockServer = mockServer;
42+
}
43+
44+
@Override
45+
public void customize(WebServiceTemplate webServiceTemplate) {
46+
Assert.state(!this.applied.getAndSet(true), "@WebServiceClientTest supports only a single WebServiceTemplate");
47+
webServiceTemplate.setMessageSender(this.mockServer.getMockMessageSender());
48+
}
49+
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test.autoconfigure.webservices.client;
18+
19+
import org.springframework.ws.test.client.MockWebServiceMessageSender;
20+
import org.springframework.ws.test.client.MockWebServiceServer;
21+
22+
/**
23+
* Test {@link MockWebServiceServer} which provides access to the underlying
24+
* {@link MockWebServiceMessageSender}.
25+
*
26+
* @author Dmytro Nosan
27+
*/
28+
final class TestMockWebServiceServer extends MockWebServiceServer {
29+
30+
private final MockWebServiceMessageSender mockMessageSender;
31+
32+
TestMockWebServiceServer(MockWebServiceMessageSender mockMessageSender) {
33+
super(mockMessageSender);
34+
this.mockMessageSender = mockMessageSender;
35+
}
36+
37+
MockWebServiceMessageSender getMockMessageSender() {
38+
return this.mockMessageSender;
39+
}
40+
41+
}

0 commit comments

Comments
 (0)