2017-01-27 65 views
0

我想用Spring WS創建一個Web服務。我遵循他們的指導,我設置了一切正常。當我運行我的應用程序併發送SOAP請求到端點時,我得到正確的響應。 (我也用Spring WS創建了一個SOAP客戶端)。所以我得到了。Spring WS集成測試沒有找到端點映射

現在我想寫測試。我有一個單元測試來測試端點的方法。一切都好,綠色。正確的返回值等。現在我也想要一個集成測試。這是它出錯的地方。我得到的錯誤: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) 

我還試圖發送Source對象與原始XML SOAP請求的內容。同樣的錯誤。我不知道我在做什麼錯誤,因爲它在運行應用程序時起作用。有任何想法嗎?

+0

我見無處端點正在創建或組件掃描您的配置......所以,我要說沒有端點正在被註冊... –

+0

但是爲什麼當我運行應用程序並使用cURL或SOAP客戶端應用程序發送請求時,它會工作? (也是我的主'應用程序'類用@SpringBootApplication註解,這是不是爲你做@ComponentScan?) – Clemenz

+0

因爲這可能有更多的配置文件,那麼只有'WebServiceConfig'和其中一個包含'@ ComponentScan'或者可能是一個帶有的xml文件,儘管如此,你的測試只加載了Spring WS基礎設施,沒有其他任何端點,因此我錯誤地註解了這個錯誤。 –

回答

0

我不知道你遵循哪個Spring教程。我跟着這一個 - https://spring.io/guides/gs/producing-web-service/。春季啓動用於和*測試,我不得不改變你的

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

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