Spring Web Service Integration Tests with @WebServiceServerTest – 使用@WebServiceServerTest的Spring Web服务集成测试

最后修改: 2022年 5月 28日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

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

在这篇文章中,我们将看到如何为使用Spring Boot构建的SOAP Web服务编写集成测试。

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.

我们已经知道如何为应用程序类编写单元测试,并且我们已经在Spring Boot 中的测试教程中介绍了一般的测试概念。因此,在这里,我们将重点讨论使用@WebServiceServerTest只对Web服务层进行集成测试。

2. Testing Spring Web Services

2.测试Spring Web服务

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.

在Spring Web服务中,端点是服务器端服务实现的关键概念。专门的@Endpoint注解将被注解的类标记为Web服务端点。重要的是,这些端点负责接收XML请求消息,调用所需的业务逻辑,并将结果作为响应消息返回

2.1. Spring Web Services Test Support

2.1.Spring Web服务测试支持

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.

为了测试这样的端点,我们可以通过传入所需的参数或模拟来轻松创建单元测试。然而,主要的缺点是,这实际上并不能测试通过电线发送的XML消息的内容。另一种方法是创建集成测试,验证消息的XML内容

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.

Spring Web Services 2.0引入了对此类端点的集成测试支持。提供这种支持的核心类是MockWebServiceClient。它提供了一个流畅的API,用于将XML消息发送到Spring应用上下文中配置的适当的端点。此外,我们可以设置响应预期,验证响应的XML,并对我们的端点进行完整的集成测试。

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

2.2.Spring Boot @WebServiceServerTest

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

Spring Boot 2.6通过@WebServiceServerTest注解扩展了Web服务测试支持。

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.

我们可以将其用于只关注Web服务层的测试,而不是加载整个应用环境。换句话说,我们可以创建一个只包含所需的@Endpoint Bean的测试片,我们可以使用@MockBean模拟任何依赖。

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

这与Spring Boot已经提供的方便的测试切片注释非常相似,例如@WebMvcTest@DataJpaTest和其他各种注释。

3. Setting up the Example Project

3.设置示例项目

3.1. Dependencies

3.1. 依赖性

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:

由于我们已经详细介绍了Spring Boot web服务项目,这里我们只包括我们项目所需的额外的测试范围spring-ws-test依赖。

<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

3.2.网络服务实例

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

接下来,让我们创建一个简单的服务,用于返回指定产品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.

在这里,我们用@Endpoint,注释了ProductEndpoint组件,该组件注册了它以处理适当的XML请求。

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.

getProduct方法接收请求对象,并在返回响应前从资源库中获取产品数据。存储库的细节在这里并不重要。在我们的案例中,我们可以使用一个简单的内存实现来保持应用程序的简单性,并专注于我们的测试策略。

4. Endpoint Testing

4.端点测试

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

最后,我们可以创建一个测试片,验证我们的XML消息在Web服务层的正确处理。

@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.

在这里,我们只为我们的集成测试配置了应用程序中带有@Endpoint注释的bean。换句话说,这个测试片创建了一个缩小的应用程序上下文。这有助于我们建立有针对性的快速集成测试,而不会因为重复加载整个应用程序上下文而导致性能下降。

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.

重要的是,该注解还配置了一个MockWebServiceClient以及其他相关的自动配置。因此,我们可以将这个客户端接入我们的测试,并使用它来发送getProductRequest XML请求,然后是各种流畅的期望。

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.

预期验证响应的XML是否根据给定的XSD模式进行验证,以及是否与预期的XML响应相匹配。我们还可以使用XPath表达式来评估和比较来自响应XML的各种值。

4.1. Endpoint Collaborators

4.1.端点合作者

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.

在我们的示例中,我们使用了@MockBean来模拟我们的ProductEndpoint中所需要的存储库。没有这个模拟,应用程序上下文就无法启动,因为完全的自动配置被禁用。换句话说,测试框架在测试执行前不会配置任何@Component@Service,@Repository Bean

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.

然而,如果我们确实需要任何实际的合作者而不是模拟,那么我们可以使用@Import来声明这些合作者。Spring会寻找这些类,然后根据需要将它们接入端点。

4.2. Loading the Whole Context

4.2.加载整个上下文

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.

如前所述,@WebServiceServerTest将不会加载整个应用程序上下文。如果我们确实需要为测试加载整个应用程序上下文,那么我们应该考虑使用@SpringBootTest@AutoConfigureMockWebServiceClient相结合。然后我们可以以类似的方式使用该客户端来发送请求并验证响应,如前所示。

5. Conclusion

5.总结

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

在这篇文章中,我们研究了Spring Boot中引入的@WebServiceServerTest注解。

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.

最初,我们谈到了Spring Boot在Web服务应用中的测试支持。接下来,我们看到了如何使用该注解为Web服务层创建测试片,这有助于建立快速而集中的集成测试。

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

像往常一样,完整的源代码可以在GitHub上找到over