Polish "Add @WebServiceServerTest slice test support"

See gh-27091
pull/27405/head
Andy Wilkinson 3 years ago
parent c897b1e689
commit 43f1b98864

@ -771,7 +771,12 @@ include::{docs-java}/features/testing/springbootapplications/autoconfiguredsprin
[[features.testing.spring-boot-applications.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.
[[features.testing.spring-boot-applications.autoconfigured-webservices.client]]
===== Auto-configured Spring Web Services Client Tests
You can use `@WebServiceClientTest` to test applications that 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 "<<io#io.webservices>>", earlier in this chapter.)
@ -782,7 +787,25 @@ The following example shows the `@WebServiceClientTest` annotation in use:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/features/testing/springbootapplications/autoconfiguredwebservices/MyWebServiceClientTests.java[]
include::{docs-java}/features/testing/springbootapplications/autoconfiguredwebservices/client/MyWebServiceClientTests.java[]
----
[[features.testing.spring-boot-applications.autoconfigured-webservices.server]]
===== Auto-configured Spring Web Services Server Tests
You can use `@WebServiceServerTest` to test applications that implement web services using the Spring Web Services project.
By default, it configures a `MockWebServiceClient` bean that can be used to call your web service endpoints.
(For more about using Web Services with Spring Boot, see "<<io#io.webservices>>", earlier in this chapter.)
TIP: A list of the auto-configuration settings that are enabled by `@WebServiceServerTest` can be <<test-auto-configuration#test-auto-configuration,found in the appendix>>.
The following example shows the `@WebServiceServerTest` annotation in use:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/features/testing/springbootapplications/autoconfiguredwebservices/server/MyWebServiceServerTests.java[]
----

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices;
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.client;
import org.junit.jupiter.api.Test;

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices;
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.client;
import javax.xml.bind.annotation.XmlRootElement;

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices;
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.client;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices;
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.client;
import org.springframework.boot.webservices.client.WebServiceTemplateBuilder;
import org.springframework.stereotype.Service;

@ -0,0 +1,35 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.server;
import javax.xml.transform.Source;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.springframework.xml.transform.StringSource;
@Endpoint
public class ExampleEndpoint {
@PayloadRoot(localPart = "ExampleRequest")
@ResponsePayload
public Source handleRequest() throws Exception {
return new StringSource("<ExampleResponse>42</ExampleResponse>");
}
}

@ -0,0 +1,43 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.docs.features.testing.springbootapplications.autoconfiguredwebservices.server;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.webservices.server.WebServiceServerTest;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.ws.test.server.RequestCreators;
import org.springframework.ws.test.server.ResponseMatchers;
import org.springframework.xml.transform.StringSource;
@WebServiceServerTest(ExampleEndpoint.class)
class MyWebServiceServerTests {
@Autowired
private MockWebServiceClient client;
@Test
void mockServerCall() {
// @formatter:off
this.client
.sendRequest(RequestCreators.withPayload(new StringSource("<ExampleRequest/>")))
.andExpect(ResponseMatchers.payload(new StringSource("<ExampleResponse>42</ExampleResponse>")));
// @formatter:on
}
}

@ -27,8 +27,8 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.ws.test.server.MockWebServiceClient;
/**
* Annotation that can be applied to a test class to enable and configure
* auto-configuration of {@link MockWebServiceClient}.
* Annotation that can be applied to a test class to enable auto-configuration of
* {@link MockWebServiceClient}.
*
* @author Daniil Razorenov
* @since 2.6.0

@ -16,9 +16,7 @@
package org.springframework.boot.test.autoconfigure.webservices.server;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -32,8 +30,7 @@ import org.springframework.ws.test.server.MockWebServiceClient;
* @since 2.6.0
*/
@Configuration(proxyBeanMethods = false)
@AutoConfigureAfter(WebServicesAutoConfiguration.class)
@ConditionalOnClass({ MockWebServiceClient.class })
@ConditionalOnClass(MockWebServiceClient.class)
public class MockWebServiceClientAutoConfiguration {
@Bean

@ -36,15 +36,15 @@ import org.springframework.test.context.BootstrapWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
/**
* Annotation that can be used for typical Spring web service server test. Can be used
* when a test focused <strong>only</strong> on Spring WS endpoints.
* Annotation that can be used for a typical Spring web service server test. Can be used
* when a test focuses <strong>only</strong> on Spring WS endpoints.
* <p>
* Using this annotation will disable full auto-configuration and instead apply only
* configuration relevant to Web Service Server tests (i.e. {@code Endpoint},
* configuration relevant to Web Service server tests (i.e. {@code Endpoint} and
* {@code EndpointInterceptor} beans but not {@code @Component}, {@code @Service} or
* {@code @Repository} beans).
* <p>
* Typically {@code WebMvcTest} is used in combination with
* Typically {@code WebServiceServerTest} is used in combination with
* {@link org.springframework.boot.test.mock.mockito.MockBean @MockBean} or
* {@link org.springframework.context.annotation.Import @Import} to create any
* collaborators required by your {@code Endpoint} beans.
@ -77,7 +77,6 @@ public @interface WebServiceServerTest {
* Properties in form {@literal key=value} that should be added to the Spring
* {@link Environment} before the test runs.
* @return the properties to add
* @since 2.1.0
*/
String[] properties() default {};
@ -103,8 +102,8 @@ public @interface WebServiceServerTest {
/**
* Determines if default filtering should be used with
* {@link SpringBootApplication @SpringBootApplication}. By default only
* {@code @Endpoint} (when no explicit {@link #endpoints() controllers} are defined),
* {@code @ControllerAdvice} and {@code WebMvcConfigurer} beans are included.
* {@code @Endpoint} (when no explicit {@link #endpoints() controllers} are defined)
* are included.
* @see #includeFilters()
* @see #excludeFilters()
* @return if default filters should be used

@ -41,7 +41,7 @@ class MockWebServiceClientAutoConfigurationTests {
}
@Test
void shouldNotRegisterMockWebServiceClientWhenItNotOnClassPath() {
void shouldNotRegisterMockWebServiceClientWhenItIsNotOnTheClasspath() {
FilteredClassLoader classLoader = new FilteredClassLoader(MockWebServiceClient.class);
this.contextRunner.withClassLoader(classLoader)

@ -43,7 +43,6 @@ public class WebServiceServerTypeExcludeFilterTests {
@Test
void matchWhenHasNoEndpoints() throws IOException {
WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(WithNoEndpoints.class);
assertThat(exclude(filter, WebService1.class)).isFalse();
assertThat(exclude(filter, WebService2.class)).isFalse();
assertThat(exclude(filter, ExampleService.class)).isTrue();
@ -53,7 +52,6 @@ public class WebServiceServerTypeExcludeFilterTests {
@Test
void matchWhenHasEndpoint() throws IOException {
WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(WithEndpoint.class);
assertThat(exclude(filter, WebService1.class)).isFalse();
assertThat(exclude(filter, WebService2.class)).isTrue();
assertThat(exclude(filter, ExampleService.class)).isTrue();
@ -63,7 +61,6 @@ public class WebServiceServerTypeExcludeFilterTests {
@Test
void matchNotUsingDefaultFilters() throws IOException {
WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(NotUsingDefaultFilters.class);
assertThat(exclude(filter, WebService1.class)).isTrue();
assertThat(exclude(filter, WebService2.class)).isTrue();
assertThat(exclude(filter, ExampleService.class)).isTrue();
@ -73,7 +70,6 @@ public class WebServiceServerTypeExcludeFilterTests {
@Test
void matchWithIncludeFilter() throws IOException {
WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(WithIncludeFilter.class);
assertThat(exclude(filter, WebService1.class)).isFalse();
assertThat(exclude(filter, WebService2.class)).isFalse();
assertThat(exclude(filter, ExampleService.class)).isTrue();
@ -83,7 +79,6 @@ public class WebServiceServerTypeExcludeFilterTests {
@Test
void matchWithExcludeFilter() throws IOException {
WebServiceServerTypeExcludeFilter filter = new WebServiceServerTypeExcludeFilter(WithExcludeFilter.class);
assertThat(exclude(filter, WebService1.class)).isTrue();
assertThat(exclude(filter, WebService2.class)).isFalse();
assertThat(exclude(filter, ExampleService.class)).isTrue();

@ -17,4 +17,5 @@ dependencies {
}
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
testImplementation("org.springframework.ws:spring-ws-test")
}

@ -0,0 +1,70 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package smoketest.webservices;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.xml.transform.stream.StreamSource;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import smoketest.webservices.service.HumanResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.webservices.server.WebServiceServerTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.ws.test.server.RequestCreators;
import org.springframework.ws.test.server.ResponseMatchers;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link SampleWsApplicationTests} that use {@link WebServiceServerTest} and
* {@link MockWebServiceClient}.
*
* @author Andy Wilkinson
*/
@WebServiceServerTest
@ExtendWith(OutputCaptureExtension.class)
class WebServiceServerTestSampleWsApplicationTests {
@MockBean
HumanResourceService service;
@Autowired
private MockWebServiceClient client;
@Test
void testSendingHolidayRequest(CapturedOutput output) throws ParseException {
String request = "<hr:HolidayRequest xmlns:hr=\"https://company.example.com/hr/schemas\">"
+ " <hr:Holiday> <hr:StartDate>2013-10-20</hr:StartDate>"
+ " <hr:EndDate>2013-11-22</hr:EndDate> </hr:Holiday> <hr:Employee>"
+ " <hr:Number>1</hr:Number> <hr:FirstName>John</hr:FirstName>"
+ " <hr:LastName>Doe</hr:LastName> </hr:Employee></hr:HolidayRequest>";
StreamSource source = new StreamSource(new StringReader(request));
this.client.sendRequest(RequestCreators.withPayload(source)).andExpect(ResponseMatchers.noFault());
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
verify(this.service).bookHoliday(dateFormat.parse("2013-10-20"), dateFormat.parse("2013-11-22"), "John Doe");
}
}
Loading…
Cancel
Save