Spring WS集成测试没有发现端点映射

时间:2017-01-27 13:17:57

标签: java spring web-services soap

我正在尝试使用Spring WS创建Web服务。我跟着他们的导游,我把一切都搞好了。当我运行我的应用程序并将SOAP请求发送到端点时,我得到了正确的响应。 (我也用Spring WS创建了一个SOAP客户端)。所以我明白了。

现在我想写测试。我有一个单元测试测试端点的方法。一切都好又绿。正确的回报价值等。现在我也想要一个集成测试。这是它出错的地方。我得到了错误:WARN org.springframework.ws.server.EndpointNotFound - No endpoint mapping found for [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)

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

1 个答案:

答案 0 :(得分:0)

我不知道您遵循哪个Spring教程。我跟着这个 - https://spring.io/guides/gs/producing-web-service/。 Spring Boot用于* Test我必须改变你的

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

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