Spring Integration – How to mock input-channel

I am new to Spring Integration so please forgive and correct me if my question is absurd. I am trying to write Unit test cases for Spring Integration application where I am testing only controller and looking to mock service call.


@PowerMockIgnore({ "javax.management.*", "javax.script.*" })
public class DocMgmtImplTestPower {

    private MockMvc mvc;

    private DocMgmtImpl docMgmtImpl;

    DocMgmtService docMgmtServiceGateway;

    SendComnMsgResponse sendComnMsgResponse;

    public void init() {
        MockitoAnnotations.initMocks(this); //
        mvc = MockMvcBuilders.standaloneSetup(DocMgmtImpl.class).build();

        PowerMockito.mockStatic(HeaderUtils.class, new Answer<Map<String, Object>>() {

            public Map<String, Object> answer(InvocationOnMock arg0) throws Throwable {
                Map<String, Object> headers = new HashMap<String, Object>();
                HeaderInfo headerInfo = new HeaderInfo();               
                headers.put(BusinessServiceConstants.SERVICE_HEADER, headerInfo);
                return headers;

    public void testMethod() throws Exception {
        SpecialFormMsgRequest arg = new SpecialFormMsgRequest();
        Map<String, Object> headers = new HashMap<String, Object>();
                (Matchers.<Map<String, Object>>any()))).thenReturn(new SendComnMsgResponse());
        SpecialFormMsgRequest msg = new SpecialFormMsgRequest();

    public static String asJsonString(final Object obj) {
        try {
            return new ObjectMapper().writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);


public class DocMgmtImpl implements DocMgmt {
    **DocMgmtService docMgmtServiceGateway;**   **// I want to mock this service.**
    @RequestMapping(value = "/specialMsg", method = RequestMethod.POST)
    public SendComnMsgResponse specialMsg(@Valid @RequestBody final SpecialFormMsgRequest specialFormMsgRequest)
            throws BusinessException, TechnicalException {

        SendComnMsgResponse sendComnMsgResponse = null;
        try {
            Map<String, Object> headers = HeaderUtils.getHeaders(poBusinessHeader); // PowerMockito working here...     
            sendComnMsgResponse = **this.docMgmtServiceGateway.specialFormMsg(specialFormMsgRequest, headers);** // docMgmtServiceGateway is getting null...
        } catch (Exception exception) {
        return sendComnMsgResponse;


<int:gateway id="docMgmtServiceGateway" service-interface="group.doc.svc.gateway.DocMgmtService"
    default-reply-channel="docReplyChannel" error-channel="docErrorChannel">    
        <int:method name="sendComnMsg" request-channel="sendComnMsgRequestChannel" />   


<int:chain input-channel="esDBBISendComnMsgRequestChannel" output-channel="docReplyChannel">
        <int:transformer method="formatRequest" ref="esSendComnMsgTransformer"/>
        <int:service-activator ref="sendComnMsgActivator" method="sendComnMsg" />
        <int:transformer method="parseResponse" ref="esSendComnMsgTransformer"/>

I am wondering, whether I am doing correct or not. Because DocMgmtService service is an interface and it don't have implementation. After controller call goes to Transformer as configured above. On this setup I have following quetions.

  1. Can I mock DocMgmtService service with same setup if not what will be correct approach.
  2. If yes then how can I mock my service. Thanks

Read more here: https://stackoverflow.com/questions/66997730/spring-integration-how-to-mock-input-channel

Content Attribution

This content was originally published by Pravin at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: