单元测试 spring 集成 dsl 时出错
Error while unit testing spring integration dsl
我在 运行 单元测试我的 spring 集成代码时看到以下错误。而且我不确定如何在使用 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();
}
}
我在你的测试中没有看到 @ContextConfiguration(classes = OrderPersistFlow.class)
class:https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/testing.html#integration-testing-annotations-spring
完全不清楚你要测试什么然后如果没有要加载的应用程序上下文...
我在 运行 单元测试我的 spring 集成代码时看到以下错误。而且我不确定如何在使用 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();
}
}
我在你的测试中没有看到 @ContextConfiguration(classes = OrderPersistFlow.class)
class:https://docs.spring.io/spring/docs/5.0.6.RELEASE/spring-framework-reference/testing.html#integration-testing-annotations-spring
完全不清楚你要测试什么然后如果没有要加载的应用程序上下文...