Spring-WS SOAP端点的单元测试在MockFilterChain中返回404失败

时间:2020-02-03 16:55:43

标签: java spring soap junit5 spring-ws

我已经使用Spring-WS编写SOAP端点,并且现在正在编写一些测试。但是,当我尝试发送请求时,我在Spring的doFilter()类的MockFilterChain中收到一个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块手动测试端点时,它工作正常。

1 个答案:

答案 0 :(得分:0)

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

您的测试将如下所示:

MessageDispatcher
相关问题