Skip to content

Commit a717fad

Browse files
committed
Add @WebServiceClientTest annotation that can be used when testing SOAP clients.
1 parent b64d8df commit a717fad

29 files changed

+1332
-2
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@
218218
<artifactId>spring-security-test</artifactId>
219219
<optional>true</optional>
220220
</dependency>
221-
221+
<dependency>
222+
<groupId>org.springframework.ws</groupId>
223+
<artifactId>spring-ws-test</artifactId>
224+
<optional>true</optional>
225+
</dependency>
222226
<!-- Test -->
223227
<dependency>
224228
<groupId>org.springframework.boot</groupId>
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.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.test.webservices.client.MockWebServiceServers;
29+
import org.springframework.ws.test.client.MockWebServiceServer;
30+
31+
/**
32+
* Annotation that can be applied to a test class to enable and configure
33+
* auto-configuration of a single {@link MockWebServiceServer}.
34+
*
35+
* @author Dmytro Nosan
36+
* @since 2.2.0
37+
*/
38+
@Target(ElementType.TYPE)
39+
@Retention(RetentionPolicy.RUNTIME)
40+
@Documented
41+
@Inherited
42+
@ImportAutoConfiguration
43+
@PropertyMapping("spring.test.webservice.client.mock-server")
44+
public @interface AutoConfigureMockWebServiceServer {
45+
46+
/**
47+
* If {@link MockWebServiceServers} bean should be registered. Defaults to
48+
* {@code true}.
49+
* @return if mock support is enabled
50+
*/
51+
boolean enabled() default true;
52+
53+
}
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.2.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,56 @@
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.AutoConfigureBefore;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
22+
import org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration;
23+
import org.springframework.boot.test.webservices.client.DefaultMockWebServiceServers;
24+
import org.springframework.boot.test.webservices.client.MockWebServiceServerRegistry;
25+
import org.springframework.boot.test.webservices.client.MockWebServiceServers;
26+
import org.springframework.boot.webservices.client.WebServiceTemplateCustomizer;
27+
import org.springframework.context.annotation.Bean;
28+
import org.springframework.context.annotation.Configuration;
29+
import org.springframework.ws.client.core.WebServiceTemplate;
30+
import org.springframework.ws.test.client.MockWebServiceServer;
31+
32+
/**
33+
* Auto-configuration for {@link MockWebServiceServers} support.
34+
*
35+
* @author Dmytro Nosan
36+
* @see AutoConfigureMockWebServiceServer
37+
* @since 2.2.0
38+
*/
39+
@Configuration(proxyBeanMethods = false)
40+
@ConditionalOnProperty(prefix = "spring.test.webservice.client.mock-server", name = "enabled")
41+
@AutoConfigureBefore(WebServiceTemplateAutoConfiguration.class)
42+
@ConditionalOnClass({ MockWebServiceServer.class, WebServiceTemplate.class })
43+
public class MockWebServiceServersAutoConfiguration {
44+
45+
@Bean
46+
public WebServiceTemplateCustomizer mockWebServiceServersWebServiceTemplateCustomizer(
47+
MockWebServiceServerRegistry registry) {
48+
return registry::bindTo;
49+
}
50+
51+
@Bean
52+
public DefaultMockWebServiceServers mockWebServiceServers() {
53+
return new DefaultMockWebServiceServers();
54+
}
55+
56+
}
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.boot.test.webservices.client.MockWebServiceServers;
20+
import org.springframework.context.ApplicationContext;
21+
import org.springframework.core.Ordered;
22+
import org.springframework.test.context.TestContext;
23+
import org.springframework.test.context.TestExecutionListener;
24+
import org.springframework.test.context.support.AbstractTestExecutionListener;
25+
import org.springframework.util.ClassUtils;
26+
27+
/**
28+
* {@link TestExecutionListener} to {@code verify} and {@code reset}
29+
* {@link MockWebServiceServers}.
30+
*
31+
* @author Dmytro Nosan
32+
* @since 2.2.0
33+
*/
34+
public class MockWebServiceServersTestExecutionListener 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(MockWebServiceServers.class, false, false);
48+
for (String name : names) {
49+
MockWebServiceServers servers = applicationContext.getBean(name, MockWebServiceServers.class);
50+
servers.verifyAll();
51+
servers.resetAll();
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,45 @@
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.Arrays;
20+
import java.util.LinkedHashSet;
21+
import java.util.Set;
22+
23+
import org.springframework.boot.context.TypeExcludeFilter;
24+
import org.springframework.boot.test.autoconfigure.filter.StandardAnnotationCustomizableTypeExcludeFilter;
25+
26+
/**
27+
* {@link TypeExcludeFilter} for {@link WebServiceClientTest @WebServiceClientTest}.
28+
*
29+
* @author Dmytro Nosan
30+
*/
31+
class WebServiceClientExcludeFilter extends StandardAnnotationCustomizableTypeExcludeFilter<WebServiceClientTest> {
32+
33+
private final Class<?>[] components;
34+
35+
protected WebServiceClientExcludeFilter(Class<?> testClass) {
36+
super(testClass);
37+
this.components = getAnnotation().getValue("components", Class[].class).orElse(new Class[0]);
38+
}
39+
40+
@Override
41+
protected Set<Class<?>> getComponentIncludes() {
42+
return new LinkedHashSet<>(Arrays.asList(this.components));
43+
}
44+
45+
}
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.AutoConfigureAfter;
20+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23+
import org.springframework.boot.autoconfigure.webservices.client.WebServiceTemplateAutoConfiguration;
24+
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.ws.client.core.WebServiceTemplate;
28+
29+
/**
30+
* Auto-configuration for a web-client {@link WebServiceTemplate}. Used when
31+
* {@link AutoConfigureWebServiceClient#registerWebServiceTemplate()} is {@code true}.
32+
*
33+
* @author Dmytro Nosan
34+
* @since 2.2.0
35+
* @see AutoConfigureWebServiceClient
36+
*/
37+
@Configuration(proxyBeanMethods = false)
38+
@ConditionalOnProperty(prefix = "spring.test.webservice.client", name = "register-web-service-template")
39+
@AutoConfigureAfter(WebServiceTemplateAutoConfiguration.class)
40+
@ConditionalOnClass(WebServiceTemplate.class)
41+
@ConditionalOnBean(WebServiceTemplateBuilder.class)
42+
public class WebServiceClientTemplateAutoConfiguration {
43+
44+
@Bean
45+
public WebServiceTemplate webServiceTemplate(WebServiceTemplateBuilder builder) {
46+
return builder.build();
47+
}
48+
49+
}

0 commit comments

Comments
 (0)