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.

Test:

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

    private MockMvc mvc;

    @InjectMocks
    private DocMgmtImpl docMgmtImpl;

    @Mock
    DocMgmtService docMgmtServiceGateway;

    @Mock
    SendComnMsgResponse sendComnMsgResponse;

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

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

            @Override
            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;
            }
        });
    }

    @SuppressWarnings("deprecation")
    @Test
    public void testMethod() throws Exception {
        SpecialFormMsgRequest arg = new SpecialFormMsgRequest();
        Map<String, Object> headers = new HashMap<String, Object>();
        Mockito.when(docMgmtServiceGateway.specialFormMsg(Mockito.any(SpecialFormMsgRequest.class),
                (Matchers.<Map<String, Object>>any()))).thenReturn(new SendComnMsgResponse());
        
        SpecialFormMsgRequest msg = new SpecialFormMsgRequest();
        msg.setUiStaticDocFlag("N");
        mvc.perform(post("/specialMsg").accept(MediaType.APPLICATION_JSON).content(asJsonString(msg))
                .contentType(MediaType.APPLICATION_JSON)).andDo(print()).andExpect(status().isOk());
    }

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

Controller:

@Controller
public class DocMgmtImpl implements DocMgmt {
    @Autowired
    **DocMgmtService docMgmtServiceGateway;**   **// I want to mock this service.**
    
    @Override
    @RequestMapping(value = "/specialMsg", method = RequestMethod.POST)
    @ResponseBody
    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) {
            handleException(exception);
        }
        return sendComnMsgResponse;
    }
}

Gateway.xml:

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

si-chain.xml:

<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"/>
</int:chain>

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: