【问题标题】:Unit testing Spring-WS SOAP endpoint fails in MockFilterChain returning 404单元测试 Spring-WS SOAP 端点在 MockFilterChain 返回 404 中失败
【发布时间】:2020-02-03 16:55:43
【问题描述】:

我已经使用 Spring-WS 编写了一个 SOAP 端点,现在正在编写一些测试。但是,当我尝试发送我的请求时,我在 Spring 的 MockFilterChain 类的 doFilter() 中出现 404,我不知道为什么。还有什么我需要模拟的吗?

我的端点是一个 SOAP 模拟器,它从我的项目中的另一个包中提取它使用的 WSDL 生成的源文件,以避免不必要的重复。所以我不能直接访问作为类路径资源的 WSDL 文件。

我的项目如下所示:

端点

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

@Endpoint
public class MockSoapEndpoint {

    private final ObjectFactory objectFactory = new ObjectFactory();

    ...
    ...

    @PayloadRoot(namespace = NAMESPACE, localPart = "soapEndpoint")
    @ResponsePayload
    public JAXBElement<MySoapResponse> chargeVolume(@RequestPayload JAXBElement<MySoapRequest> request) {
        ...
        ...
    }

配置

@EnableWs
@Configuration
public class MockSoapConfig extends WsConfigurerAdapter {

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(final ApplicationContext applicationContext) {
        MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
        messageDispatcherServlet.setApplicationContext(applicationContext);
        messageDispatcherServlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(messageDispatcherServlet, "/ws/*");
    }
}

测试

@WebMvcTest
@ContextConfiguration(classes = { MockSoapConfig.class, MockSoapEndpoint.class })
@ExtendWith(SpringExtension.class)
class MockSoapEndpointTest {

    private static final String SOAP_REQUEST = ""
                    + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n"
                    + "<soapenv:Header/>..."</soapenv:Envelope>\n";

    @Autowired
    private MockMvc mockMvc;

    @Test
    void givenNonRejectedMdnReturnChargingInformationWithCorrectMinMajCodes() throws Exception {

        final String endpoint = "http://localhost:8080/ws";

        mockMvc.perform(post(endpoint).contentType(MediaType.TEXT_XML)
                                  .content(SOAP_REQUEST)
                                  .accept(MediaType.TEXT_XML))
               .andExpect(status().isOk());
    }
}

此测试返回 404 响应代码,而不是我期待的 200。当我使用 http://localhost:8080/ws 和相同的 XML 块手动测试端点时,它工作正常。

【问题讨论】:

  • 它不是一个`@WebMvcTest`。您正在测试 Soap Webservice 而不是控制器等。

标签: java spring soap junit5 spring-ws


【解决方案1】:

您应该使用MockWebServiceClient 而不是MockMvc。 正如提到的hereMockMvc 仅扫描@RestController/@Controller 并省略@Endpoint,因此您的映射/ws/* 不存在并且您没有正确的MessageDispatcher

您的测试将如下所示:

 import org.springframework.xml.transform.StringSource;
 import org.springframework.ws.test.server.MockWebServiceClient;
 import static org.springframework.ws.test.server.RequestCreators.*;
 import static org.springframework.ws.test.server.ResponseMatchers.*;
 
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration("applicationContext.xml")
 public class MyWebServiceIntegrationTest {

         // a standard MessageDispatcherServlet application context, containing endpoints, mappings, etc.
         @Autowired
         private ApplicationContext applicationContext;

         private MockWebServiceClient mockClient;

         @Before
         public void createClient() throws Exception {
           mockClient = MockWebServiceClient.createClient(applicationContext);
         }

         // test the CustomerCountEndpoint, which is wired up in the application context above
         // and handles <customerCount/> messages
         @Test
         public void customerCountEndpoint() throws Exception {
           Source requestPayload = new StringSource(
                 "<customerCountRequest xmlns='http://springframework.org/spring-ws'>" +
                 "<customerName>John Doe</customerName>" +
                 "</customerCountRequest>");
           Source expectedResponsePayload = new StringSource(
                 "<customerCountResponse xmlns='http://springframework.org/spring-ws'>" +
                 "<customerCount>42</customerCount>" +
                 "</customerCountResponse>");

           mockClient.sendRequest(withPayload(requestPayload)).andExpect(payload(expectedResponsePayload));
         }
 }

【讨论】:

猜你喜欢
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多