简体   繁体   中英

Mock spring integration outbound web service call

I have spring integration project with outbound soap web service call.

I have integration test case that tests input and output of the project where input is being read from active mq queue and output is sent to database column.

This value written to database is returned by SOAP web service.

Below is web service call

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

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

how do I avoid calling actual live web service and instead mock the web service call and return static response ?

Give the chain and gateway id attributes:

<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> 

You can then @Autowire your test with the AbstractWebServiceOutboundGateway with @Qualifier("soapcall$child.wsgw.handler") .

Then, in your test, setMessageSender(myMockWebServiceMessageSender) .

Thanks to Gary's answer and this sample here

Using Spring Boot and Mockito, I was able to mock the soap outbound gateway with a static response :

@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
        // ...
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM