I am currently learning spring integration framework. So please forgive and correct me if my question is absurd. how to or is there a way to mock an http outbound gateway to run my unit tastcase?
public IntegrationFlow sendSms() {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
final JsonObjectMapper<?, ?> mapper = new Jackson2JsonObjectMapper(objectMapper);
return IntegrationFlows.from("sendSmsGatewayRequestChannel")
.handle(Http
.outboundGateway(environment.getProperty("integration.sms.endpoint")
+ "?ProductType={ProductType}&MobleNo={MobleNo}&smsType={smsType}&Message={Message}")
.expectedResponseType(String.class).httpMethod(HttpMethod.GET)
.uriVariableExpressions(createExpressionMap()))
.transform(Transformers.fromJson(SmsResponseDto.class, mapper)).get();
}
this is my integration flow.
Also, can we use mockmvc for testing inbound gateway?
add a bean id to the handler as,
@Bean
public IntegrationFlow sendSms() {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
final JsonObjectMapper<?, ?> mapper = new Jackson2JsonObjectMapper(objectMapper);
return IntegrationFlows.from("sendSmsGatewayRequestChannel")
.handle(Http.outboundGateway(smsEndpoint).expectedResponseType(String.class).httpMethod(HttpMethod.GET)
.uriVariableExpressions(createExpressionMap()), **e -> e.id("smsOutboundGateway")**)
.transform(Transformers.fromJson(SmsResponseDto.class, mapper)).get();
}
then you can mock the response by,
final ArgumentCaptor<Message<?>> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
final MessageHandler mockMessageHandler = MockIntegration.mockMessageHandler(messageArgumentCaptor)
.handleNextAndReply(m -> MessageBuilder.withPayload(
"response")
.build());
mockIntegrationContext.substituteMessageHandlerFor("smsOutboundGateway", mockMessageHandler);
Note: I am new to spring integration. so i don't know whether this is the correct way to do it. anyways, it's working :D. correct if i'm wrong. :)
Thanks Artem Bilan for the help.
Your code and logic is fully correct. Since
Http.outboundGateway()
is based on theHttpRequestExecutingMessageHandler
which is essentially aMessageHandler
implementation, it is really OK to usesubstituteMessageHandlerFor
to mock that HTTP requests. The same can be done in any integration point of your flow.Always welcome! Time to consider this then: stackoverflow.com/help/someone-answers
i have created an actual bean of HttpRequestExecutingMessageHandler . is there any way to mock its response like the above method? i am not able to use mockIntegrationContext.substituteMessageHandlerFor. what is expected for the substitition is IntegrationConsumer bean. instead of HttpRequestExecutingMessageHandler bean. is there any way to mock it?
See my answer then: the Mock MVC is for you over there. Although you still can use substitute if you call it via sending messages to the channel of the endpoint you want to mock.