Search code examples
spring-integrationspring-integration-dsl

Error while unit testing spring integration dsl


I am seeing following error while running the unit testing my spring integration code. And also i was not sure how to mock my service and other dependencies while using mockIntegrationContext.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'orderInputEndPoint' available

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:686)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1210)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:204)

Main program code

@EnableIntegration
public class OrderPersistFlow {

    @Autowired
    private ActiveMQConnectionFactory activeMQConnectionFactory;

    @Autowired
    private OrderTransformer orderTransformer;

    @Autowired
    private OrderService orderService;


    @Bean
    public IntegrationFlow persistFlow() {
        return IntegrationFlows
                .from(Jms.messageDrivenChannelAdapter(activeMQConnectionFactory)
                        .id("orderInputEndPoint")
                        .destination("order.queue")
                        .jmsMessageConverter(new MarshallingMessageConverter(jaxbMarshaller())))
                .filter(OrderVO.class, p -> p.getOrderStatus().equals("OPEN")
                .transform(orderTransformer)
                .handle(orderService, "save")
                .get();
    }
}

Test code

RunWith(SpringRunner.class)
@SpringIntegrationTest(noAutoStartup = "orderInputEndPoint")
public class OrderPersistFlowTest {

    @Autowired
    private MockIntegrationContext mockIntegrationContext;

    @Test
    public void persistFlowTest(){
        OrderVO orderVO = new OrderVO();
        orderVO.setId("1234");
        orderVO.setName("TestOrder");
        orderVO.setDescription("order desc");

        MessageSource<OrderVO> messageSource = () -> new GenericMessage<>(orderVO);

        this.mockIntegrationContext.substituteMessageSourceFor("orderInputEndPoint", messageSource);

        Message<?> receive = messageSource.receive();
    }
}

Solution

  • I don't see a @ContextConfiguration(classes = OrderPersistFlow.class) on your test class: https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/testing.html#integration-testing-annotations-spring

    Fully not clear what you are going to test then if there is no application context to load...