Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

In this article, we’ll see how to write integration tests for SOAP web services built using Spring Boot.

We already know how to write unit tests for the application classes, and we’ve already covered general testing concepts in our tutorial on testing in Spring Boot. So here, we’ll focus on integration testing just the web service layer using the @WebServiceServerTest.

2. Testing Spring Web Services

In Spring Web Services, endpoints are the key concept for server-side service implementation. The specialized @Endpoint annotation marks the annotated class as a web service endpoint. Importantly, these endpoints are responsible for receiving the XML request messages, invoking the required business logic, and returning the result as a response message.

2.1. Spring Web Services Test Support

In order to test such endpoints, we can easily create unit tests by passing in the required arguments or mocks. However, the major disadvantage is that this does not actually test the content of XML messages sent over the wire. The alternative approach is to create integration tests that do verify the XML content of the messages.

Spring Web Services 2.0 introduced support for integration testing of such endpoints. The core class that provides this support is MockWebServiceClient. It provides a fluent API to send XML messages to the appropriate endpoint as configured in the Spring application context. In addition, we can set up response expectations, verify the response XML, and perform a complete integration test for our endpoint.

However, this requires bringing up the entire application context, which slows the test execution. This is often undesirable, especially if we’re looking to create fast and isolated tests for particular web service endpoints.

2.2. Spring Boot @WebServiceServerTest

Spring Boot 2.6 has extended the web service testing support with the @WebServiceServerTest annotation.

We can use this for tests focusing only on the web service layer rather than loading the whole application context. In other words, we can create a test slice containing only the required @Endpoint beans, and we can mock any dependencies using @MockBean.

This is very similar to the handy test slice annotations already provided by Spring Boot, such as @WebMvcTest, @DataJpaTest, and various others.

3. Setting up the Example Project

3.1. Dependencies

As we’ve already covered a Spring Boot web service project in detail, here we’ll just include the additional test-scoped spring-ws-test dependency required for our project:

<dependency>
    <groupId>org.springframework.ws</groupId>
    <artifactId>spring-ws-test</artifactId>
    <version>4.0.10</version>
    <scope>test</scope>
</dependency>

3.2. Example Web Service

Next, let’s create a simple service for returning some product data for the specified product id:

@Endpoint
public class ProductEndpoint {

    @Autowired
    private ProductRepository productRepository;

    @ResponsePayload
    public GetProductResponse getProduct(@RequestPayload GetProductRequest request) {
        GetProductResponse response = new GetProductResponse();
        response.setProduct(productRepository.findProduct(request.getId()));
        return response;
    }
}

Here, we’ve annotated the ProductEndpoint component with @Endpoint, which registers it for handling the appropriate XML request.

The getProduct method receives the request object and obtains the product data from a repository before returning the response. The details of the repository are not important here. In our case, we can use a simple in-memory implementation to keep the application simple and focus on our testing strategy.

4. Endpoint Testing

Finally, we can create a test slice and verify the correct processing of our XML messages in the web service layer:

@WebServiceServerTest
class ProductEndpointIntegrationTest {

    @Autowired
    private MockWebServiceClient client;

    @MockBean
    private ProductRepository productRepository;

    @Test
    void givenXmlRequest_whenServiceInvoked_thenValidResponse() throws IOException {
        Product product = createProduct();
        when(productRepository.findProduct("1")).thenReturn(product);

        StringSource request = new StringSource(
          "<bd:getProductRequest xmlns:bd='http://baeldung.com/spring-boot-web-service'>" + 
            "<bd:id>1</bd:id>" + 
          "</bd:getProductRequest>"
        );
        
        StringSource expectedResponse = new StringSource(
          "<bd:getProductResponse xmlns:bd='http://baeldung.com/spring-boot-web-service'>" + 
            "<bd:product>" + 
              "<bd:id>1</bd:id>" + 
              "<bd:name>Product 1</bd:name>" + 
            "</bd:product>" + 
          "</bd:getProductResponse>"
        );

        client.sendRequest(withPayload(request))
          .andExpect(noFault())
          .andExpect(validPayload(new ClassPathResource("webservice/products.xsd")))
          .andExpect(payload(expectedResponse))
          .andExpect(xpath("/bd:getProductResponse/bd:product[1]/bd:name", NAMESPACE_MAPPING)
            .evaluatesTo("Product 1"));
    }
}

Here, we’ve only configured the beans annotated with @Endpoint in the application for our integration test. In other words, this test slice creates a reduced application context. This helps us to build targeted and fast integration tests without the performance penalties associated with repeatedly loading the whole application context.

Importantly, this annotation also configures a MockWebServiceClient along with other relevant auto-configurations. As a result, we can wire this client into our tests and use it to send the getProductRequest XML request, followed by various fluent expectations.

The expectations verify that the response XML validates against the given XSD schema and that it matches the expected XML response. We can also use XPath expressions to evaluate and compare various values from the response XML.

4.1. Endpoint Collaborators

In our example, we’ve used @MockBean for mocking the repository required in our ProductEndpoint. Without this mock, the application context cannot start as full auto-configuration is disabled. In other words, the test framework does not configure any @Component, @Service, or @Repository beans before test execution.

However, if we do require any actual collaborators instead of the mocks, then we can declare these using @Import. Spring will look for these classes and then wire them into the endpoints, as required.

4.2. Loading the Whole Context

As mentioned previously, @WebServiceServerTest will not load the whole application context. If we do need to load the entire application context for the test, then we should consider using the @SpringBootTest combined with the @AutoConfigureMockWebServiceClient. We can then use this client in a similar fashion to send the request and verify the response, as shown previously.

5. Conclusion

In this article, we looked at the @WebServiceServerTest annotation introduced in Spring Boot.

Initially, we talked about Spring Boot testing support in a web services application. Following on, we saw how to create a test slice for the web service layer using this annotation, which helps to build fast and focused integration tests.

As usual, the full source code is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!