Partner – Jmix-Haulmont – NPI (cat= Spring Boot)
announcement - icon

Whether you're just starting out or have years of experience, Spring Boot is obviously a great choice for building a web application.

Jmix builds on this highly powerful and mature Boot stack, allowing devs to build and deliver full-stack web applications without having to code the frontend. Quite flexibly as well, from simple web GUI CRUD applications to complex enterprise solutions.

Concretely, The Jmix Platform includes a framework built on top of Spring Boot, JPA, and Vaadin, and comes with Jmix Studio, an IntelliJ IDEA plugin equipped with a suite of developer productivity tools.

The platform comes with interconnected out-of-the-box add-ons for report generation, BPM, maps, instant web app generation from a DB, and quite a bit more:

>> Become an efficient full-stack developer with Jmix

Course – RWSB – NPI (cat=REST/Spring Boot)
announcement - icon

Now that the new version of REST With Spring - “REST With Spring Boot” is finally out, the current price will be available until this Friday, after which it will permanently increase by 50$


Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring 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:


3.2. Example Web Service

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

public class ProductEndpoint {

    private ProductRepository productRepository;

    public GetProductResponse getProduct(@RequestPayload GetProductRequest request) {
        GetProductResponse response = new GetProductResponse();
        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:

class ProductEndpointIntegrationTest {

    private MockWebServiceClient client;

    private ProductRepository productRepository;

    void givenXmlRequest_whenServiceInvoked_thenValidResponse() throws IOException {
        Product product = createProduct();

        StringSource request = new StringSource(
          "<bd:getProductRequest xmlns:bd=''>" + 
            "<bd:id>1</bd:id>" + 
        StringSource expectedResponse = new StringSource(
          "<bd:getProductResponse xmlns:bd=''>" + 
            "<bd:product>" + 
              "<bd:id>1</bd:id>" + 
              "<bd:name>Product 1</bd:name>" + 
            "</bd:product>" + 

          .andExpect(validPayload(new ClassPathResource("webservice/products.xsd")))
          .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 – RWSB – NPI (cat=REST/Spring Boot)
announcement - icon

Now that the new version of REST With Spring - “REST With Spring Boot” is finally out, the current price will be available until this Friday, after which it will permanently increase by 50$


Course – LS – All
announcement - icon

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


res – REST with Spring (eBook) (everywhere)