Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

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>3.1.3</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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until next Friday:

>> GET ACCESS NOW

Junit footer banner
Comments are closed on this article!