Skip to content

Commit 1c8f362

Browse files
committed
Add @WebServiceClientTest annotation that can be used
when testing SOAP clients
1 parent be938f0 commit 1c8f362

25 files changed

+1141
-2
lines changed

spring-boot-project/spring-boot-docs/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@
10961096
</dependency>
10971097
<dependency>
10981098
<groupId>org.springframework.ws</groupId>
1099-
<artifactId>spring-ws-core</artifactId>
1099+
<artifactId>spring-ws-test</artifactId>
11001100
<optional>true</optional>
11011101
</dependency>
11021102
<dependency>

spring-boot-project/spring-boot-test-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,11 @@
223223
<artifactId>spring-security-test</artifactId>
224224
<optional>true</optional>
225225
</dependency>
226+
<dependency>
227+
<groupId>org.springframework.ws</groupId>
228+
<artifactId>spring-ws-test</artifactId>
229+
<optional>true</optional>
230+
</dependency>
226231
<!-- Test -->
227232
<dependency>
228233
<groupId>org.springframework.boot</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2019 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.mock-server")
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-2019 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,49 @@
1+
/*
2+
* Copyright 2012-2019 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.MockWebServiceServer;
25+
26+
/**
27+
* Auto-configuration for {@link MockWebServiceServer} support.
28+
*
29+
* @author Dmytro Nosan
30+
* @see AutoConfigureMockWebServiceServer
31+
* @since 2.3.0
32+
*/
33+
@Configuration(proxyBeanMethods = false)
34+
@ConditionalOnProperty(prefix = "spring.test.webservice.client.mock-server", name = "enabled")
35+
@ConditionalOnClass({ MockWebServiceServer.class, WebServiceTemplate.class })
36+
public class MockWebServiceServerAutoConfiguration {
37+
38+
@Bean
39+
public MockWebServiceServer mockWebServiceServer() {
40+
return MockWebServiceServerUtils.createMockServer();
41+
}
42+
43+
@Bean
44+
public MockWebServiceServerWebServiceTemplateCustomizer mockWebServiceServerWebServiceTemplateCustomizer(
45+
MockWebServiceServer mockWebServiceServer) {
46+
return new MockWebServiceServerWebServiceTemplateCustomizer(mockWebServiceServer);
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2012-2019 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+
MockWebServiceServerUtils.reset(mockServer);
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,98 @@
1+
/*
2+
* Copyright 2012-2019 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.reflect.Constructor;
20+
import java.lang.reflect.Field;
21+
import java.util.List;
22+
23+
import org.springframework.beans.BeanUtils;
24+
import org.springframework.util.Assert;
25+
import org.springframework.util.ClassUtils;
26+
import org.springframework.util.ReflectionUtils;
27+
import org.springframework.ws.test.client.MockWebServiceServer;
28+
import org.springframework.ws.transport.WebServiceMessageSender;
29+
30+
/**
31+
* Utility class for dealing with {@link MockWebServiceServer}.
32+
*
33+
* @author Dmytro Nosan
34+
*/
35+
abstract class MockWebServiceServerUtils {
36+
37+
/**
38+
* Reset the given mock server.
39+
* @param mockWebServiceServer the mock server
40+
*/
41+
static void reset(MockWebServiceServer mockWebServiceServer) {
42+
WebServiceMessageSender mockSender = getMockSender(mockWebServiceServer);
43+
((List<?>) getField(mockSender, "expectedConnections")).clear();
44+
setField(mockSender, "connectionIterator", null);
45+
}
46+
47+
/**
48+
* Creates a deferred mock server.
49+
* @return the mock server
50+
*/
51+
static MockWebServiceServer createMockServer() {
52+
try {
53+
WebServiceMessageSender mockSender = createMockWebServiceMessageSender();
54+
return newInstance(MockWebServiceServer.class, new Class[] { mockSender.getClass() },
55+
new Object[] { mockSender });
56+
}
57+
catch (Exception ex) {
58+
throw new IllegalStateException(ex);
59+
}
60+
}
61+
62+
/**
63+
* Return the mock message sender from the given server.
64+
* @param mockWebServiceServer the mock server
65+
* @return the mock web sender
66+
*/
67+
static WebServiceMessageSender getMockSender(MockWebServiceServer mockWebServiceServer) {
68+
return (WebServiceMessageSender) getField(mockWebServiceServer, "mockMessageSender");
69+
}
70+
71+
private static WebServiceMessageSender createMockWebServiceMessageSender() throws Exception {
72+
ClassLoader cl = MockWebServiceServerUtils.class.getClassLoader();
73+
Class<?> targetClass = ClassUtils.forName("org.springframework.ws.test.client.MockWebServiceMessageSender", cl);
74+
return (WebServiceMessageSender) newInstance(targetClass, new Class[0], new Object[0]);
75+
}
76+
77+
private static <T> T newInstance(Class<? extends T> targetClass, Class<?>[] parameterTypes, Object[] arguments)
78+
throws Exception {
79+
Constructor<? extends T> constructor = ReflectionUtils.accessibleConstructor(targetClass, parameterTypes);
80+
return BeanUtils.instantiateClass(constructor, arguments);
81+
}
82+
83+
private static Object getField(Object target, String name) {
84+
return ReflectionUtils.getField(findField(target, name), target);
85+
}
86+
87+
private static void setField(Object target, String name, Object value) {
88+
ReflectionUtils.setField(findField(target, name), target, value);
89+
}
90+
91+
private static Field findField(Object target, String name) {
92+
Field field = ReflectionUtils.findField(target.getClass(), name);
93+
Assert.state(field != null, "Could not find a field [" + name + "] for [" + target + "]");
94+
ReflectionUtils.makeAccessible(field);
95+
return field;
96+
}
97+
98+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2019 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.ws.client.core.WebServiceTemplate;
24+
import org.springframework.ws.test.client.MockWebServiceServer;
25+
26+
/**
27+
* {@link WebServiceTemplateCustomizer} that can be applied to a
28+
* {@link WebServiceTemplateBuilder} instances to add {@link MockWebServiceServer}
29+
* support.
30+
*
31+
* @author Dmytro Nosan
32+
*/
33+
class MockWebServiceServerWebServiceTemplateCustomizer implements WebServiceTemplateCustomizer {
34+
35+
private final AtomicBoolean alreadySet = new AtomicBoolean();
36+
37+
private final MockWebServiceServer mockServer;
38+
39+
MockWebServiceServerWebServiceTemplateCustomizer(MockWebServiceServer mockServer) {
40+
this.mockServer = mockServer;
41+
}
42+
43+
@Override
44+
public void customize(WebServiceTemplate webServiceTemplate) {
45+
if (this.alreadySet.compareAndSet(false, true)) {
46+
webServiceTemplate.setMessageSender(MockWebServiceServerUtils.getMockSender(this.mockServer));
47+
}
48+
else {
49+
throw new IllegalStateException("@WebServiceClientTest supports only a single WebServiceTemplate");
50+
}
51+
}
52+
53+
}

0 commit comments

Comments
 (0)