如何在 Spring 集成测试中调用通道

nag*_*dra 3 spring-integration spring-integration-dsl

我有一个接受字符串输入的 Flow

  @Bean
  public IntegrationFlow myFlow() {
        // @formatter:off
        return IntegrationFlows.from("some.input.channel")
                               .handle(someService)
                               .get();
Run Code Online (Sandbox Code Playgroud)

我如何从我的集成测试中调用它,如何在“some.input.channel”上放置一个字符串消息

Art*_*lan 6

Read Javadocs of the API you use:

/**
 * Populate the {@link MessageChannel} name to the new {@link IntegrationFlowBuilder} chain.
 * The {@link org.springframework.integration.dsl.IntegrationFlow} {@code inputChannel}.
 * @param messageChannelName the name of existing {@link MessageChannel} bean.
 * The new {@link DirectChannel} bean will be created on context startup
 * if there is no bean with this name.
 * @return new {@link IntegrationFlowBuilder}.
 */
public static IntegrationFlowBuilder from(String messageChannelName) {
Run Code Online (Sandbox Code Playgroud)

Then open a Reference Manual of the Framework you use:

https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-channels-section.html#messaging-channels-section

https://docs.spring.io/spring/docs/current/spring-framework-reference/testing.html#integration-testing-annotations-standard

因此,Java DSL 创建的通道成为应用程序上下文中的 bean。将它自动连接到测试类并send()从测试方法调用它就足够了:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = MyFlowConfiguration.class)
public class IntegrationFlowTests {

    @Autowired
    @Qualifier("some.input.channel")
    private MessageChannel someInputChannel;

    @Test
    public void myTest() {
          this.someInputChannel.send(new GenericMessage<>("foo"));
    }
}
Run Code Online (Sandbox Code Playgroud)