2016-09-21 65 views
0

我有出站SOAP Web服務調用的Spring集成項目。模擬春季集成出站Web服務調用

我有集成測試用例,用於測試項目的輸入和輸出,其中正在從活動mq隊列讀取輸入,並將輸出發送到數據庫列。

寫入數據庫的該值由SOAP Web服務返回。

下面是Web服務調用

<int:chain id="soapcall" input-channel="soapChannel"> 
    <int:service-activator ref="convertData" /> 
    <int-ws:outbound-gateway uri="${url}" 

    </int-ws:outbound-gateway> 
</int:chain> 

我該如何避免調用實際的實時Web服務,而是嘲笑Web服務調用和返回靜態響應?

回答

0

給鏈和網關id屬性:

<int:chain id="soapcall" input-channel="soapChannel"> 
    <int:service-activator ref="convertData" /> 
    <int-ws:outbound-gateway id="wsgw" uri="${url}" 

    </int-ws:outbound-gateway> 
</int:chain> 

然後,您可以@Autowire您與AbstractWebServiceOutboundGateway@Qualifier("soapcall$child.wsgw.handler")測試。

然後,在你的測試中,setMessageSender(myMockWebServiceMessageSender)

0

感謝Gary的answer這個樣本here

使用Spring和引導的Mockito,我可以用一個靜態響應嘲笑肥皂站網關:

@SpringBootTest 
@RunWith(SpringRunner.class) 
public class SoapGatewayTest { 

    private static final String XML_REPONSE = "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Body>" 
      // enrich your answer here 
      // ... 
      + "</S:Body></S:Envelope>"; 

    @Autowired 
    @Qualifier("soapcall$child.wsgw.handler") 
    private AbstractWebServiceOutboundGateway simpleWebServiceOutboundGateway; 

    @Mock 
    private WebServiceMessageSender messageSender; 

    @Mock 
    private WebServiceConnection wsConnection; 

    @Test 
    public void myMethodTest() { 
     // mocking the WS SOAP gateway 
     when(this.messageSender.createConnection(any(URI.class))).thenReturn(this.wsConnection); 
     when(this.messageSender.supports(any(URI.class))).thenReturn(true); 

     // the gateway will always respond with a static response 
     doAnswer(new Answer<WebServiceMessage>() { 
      public WebServiceMessage answer(InvocationOnMock invocation) throws InvalidXmlException, IOException { 
       WebServiceMessageFactory factory = invocation.getArgumentAt(0, WebServiceMessageFactory.class); 
       return factory.createWebServiceMessage(new ByteArrayInputStream(XML_REPONSE.getBytes())); 
      } 
     }).when(this.wsConnection).receive(any(WebServiceMessageFactory.class)); 

     this.simpleWebServiceOutboundGateway.setMessageSender(this.messageSender); 

     // run the code to be tested here 
     // ... 
    } 
}