单元测试弹簧集成dsl时出错

时间:2018-06-08 10:37:59

标签: spring-integration spring-integration-dsl

我在运行测试弹簧集成代码的单元时看到以下错误。而且我也不确定在使用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)

主程序代码

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

测试代码

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();
    }
}

1 个答案:

答案 0 :(得分:2)

我的测试类上没有@ContextConfiguration(classes = OrderPersistFlow.class)https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/testing.html#integration-testing-annotations-spring

如果没有要加载的应用程序上下文,则完全不清楚您要测试的内容...