【发布时间】: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