2

i was unable to find an simple example to unit test the spring integration dsl, which involves picking up a message from queue and making a rest call.

I looked at the examples https://github.com/spring-projects/spring-integration-java-dsl but was not clear on qualifiers etc for the below code for which i want to write unit test on.

 IntegrationFlows.from(Jms.inboundGateway(connectionFactory) .id("inputChannel") .destination(sourceQueue) .jmsMessageConverter(new MarshallingMessageConverter(jaxbMarshaller()))) .something to validate and route .handle(Http.outboundGateway("http://localhost:9999/create) .httpMethod(HttpMethod.POST) .expectedResponseType(String.class)) .get(); 

1 Answer 1

2

Something else is needed in your question to explain more the requirements.

Anyway I'll try to answer in my best feeling on the matter.

Spring Integration Java DSL is nothing more then codding tool to wire beans and build integration components into flows. In the end, at runtime, we just have a set of beans with which we can interact as with any other beans in the application context.

So, if the story is about consuming some destination from JMS and verify what we get from there, there is just enough to run ActiveMQ in the embedded mode - it is as simple as bean for:

new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false") 

Then you use JmsTemplate to send some test data to the desired destination (will be created on demand) and consume an Integration message from the channel defined in the mentioned in your question IntegrationFlow.

Typically for consuming test data we use a QueueChannel and its receive(long timeout). This way we block a unit test until data arrives or timeout is elapsed.

Another way to verify a flow work is with the Spring Integration Testing Framework. From there you can use a MockIntegration to replace the real MessageHandler in the application context and verify an interaction with the mock afterward.

Hope that helps a bit.

Sign up to request clarification or add additional context in comments.

8 Comments

I am planning for mock test, thanks for pointing to right documentation. I updated the above example, in the example i am trying to replace jms input for which i wrote the below code MessageSource<String> messageSource = () -> new GenericMessage<>("some payload"); mockIntegrationContext.substituteMessageSourceFor("inputChannel", messageSource); I am seeing the following error org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'inputChannel' available How to i refer to input channel
That's because Jms.inboundGateway() is not a MessageSource. It is a MessagingGatewaySupport. You can overcome the problem anyway: 1. You need @SpringIntegrationTest(noAutoStartup = "inputChannel"). 2. You really need to add a real channel before the next .something to validate and route. 3. Use your mock messageSource to call its receive() and send the result message to the mentioned channel. I think we might consider to add to the MockIntegrationContext something like substituteMessagingGatewayWithMessagesource() to let it work automatically without inserting extra channel
I started writing the integration test using the embedded activemq broker, i am successful in placing the message using jmsTemplate and i can see the message is picked and saved in db. But i am unable to verify the contents of db record as my test are unable to see the record written by integration flow as its in different transaction i believe.
Well, on this case we can help only if there is a code to play. Although that already doesn’t sound like a unit test and is not related to a general question above...
My unit & integration tests are github.com/NagendraSiram/spring-integration-sample Error description is in the java test code comment
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.