gpt4 book ai didi

java - Spring WS集成测试未找到端点映射

转载 作者:行者123 更新时间:2023-11-30 06:55:37 27 4
gpt4 key购买 nike

我正在尝试使用 Spring WS 创建 Web 服务。我按照他们的指导,一切都设置得很好。当我运行应用程序并向端点发送 SOAP 请求时,我得到了正确的响应。 (我也使用 Spring WS 创建了一个 SOAP 客户端)。所以我就这么做了。

现在我想编写测试。我进行了单元测试来测试端点的方法。一切都好,绿色。对的返回值等等。现在我还想要一个集成测试。这就是出错的地方。我收到错误:WARN org.springframework.ws.server.EndpointNotFound - 找不到 [SaajSoapMessage {http://my.app/soap/version}GetServiceVersionRequest] 的端点映射(必须审查一些变量和命名有原因。可能是它们不匹配。如果发生我会修复它。在实际情况下它是有效的。)

我有以下配置:

@EnableWs
@Configuration
public class WebServiceConfig {

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

@Bean(name = "serviceVersion")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema serviceVersionSchema) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("ServiceVersionPort");
wsdl11Definition.setLocationUri("/ws/version");
wsdl11Definition.setTargetNamespace("http://my.app/soap/version");
wsdl11Definition.setSchema(serviceVersionSchema);
return wsdl11Definition;
}

@Bean
public XsdSchema serviceVersionSchema() {
return new SimpleXsdSchema(new ClassPathResource("xsd/ServiceVersionRequest.xsd"));
}
}

我有以下端点:

@Endpoint
public class GetServiceVersionEndpoint {

@PayloadRoot(namespace = "http://my.app/soap/version", localPart = "GetServiceVersionRequest")
@ResponsePayload
public JAXBElement<GetServiceVersionResponse> getServiceVersion(@RequestPayload GetServiceVersionRequest request) {
GetServiceVersionResponse response = new GetServiceVersionResponse();
ServiceVersion serviceVersion = new ServiceVersion();
serviceVersion.setMajor(1);
serviceVersion.setMinor(0);
serviceVersion.setRevision(0);
serviceVersion.setBuild(1);
response.setVersion(serviceVersion);
return new ObjectFactory().createGetServiceVersionResponse(response);
}

}

这是我的测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebServiceConfig.class)
public class GetServiceVersionEndpointIntegrationTest {

@Inject
private ApplicationContext applicationContext;

private MockWebServiceClient mockClient;

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

@Test
public void testValidServiceVersionRequest() throws Exception {

JAXBElement<GetServiceVersionRequest> request = new ObjectFactory().createGetServiceVersionRequest(new GetServiceVersionRequest());

ServiceVersion serviceVersion = new ServiceVersion();
serviceVersion.setMajor(1);
serviceVersion.setMinor(0);
serviceVersion.setRevision(0);
serviceVersion.setBuild(1);
GetServiceVersionResponse getServiceVersionResponse = new GetServiceVersionResponse();
getServiceVersionResponse.setVersion(serviceVersion);

JAXBElement<GetServiceVersionResponse> response = new ObjectFactory().createGetServiceVersionResponse(getServiceVersionResponse);

mockClient
.sendRequest(withPayload(new JAXBSource(JAXBContext.newInstance(GetServiceVersionRequest.class), request)))
.andExpect(payload(new JAXBSource(JAXBContext.newInstance(GetServiceVersionResponse.class), response)));

}
}

堆栈跟踪是:

java.lang.AssertionError: No endpoint can be found for request [SaajSoapMessage {http://my.app/soap/version}GetServiceVersionRequest]

at org.springframework.ws.test.support.AssertionErrors.fail(AssertionErrors.java:39)
at org.springframework.ws.test.server.MockWebServiceClient.sendRequest(MockWebServiceClient.java:184)
at nl.app.endpoint.GetServiceVersionEndpointIntegrationTest.testValidServiceVersionRequest(GetServiceVersionEndpointIntegrationTest.java:78)

我还尝试发送一个 Source 对象,其中包含原始 XML SOAP 请求作为内容。同样的错误。我不知道我做错了什么,因为它在运行应用程序时有效。有什么想法吗?

最佳答案

我不知道你遵循的是哪个 Spring 教程。我关注了这个 - https://spring.io/guides/gs/producing-web-service/ 。使用 Spring Boot,为了*测试,我必须更改您的

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = WebServiceConfig.class)

@RunWith(SpringJUnit4ClassRunner.class) // same
@SpringBootTest

关于java - Spring WS集成测试未找到端点映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41894887/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com