等待executorChannel在Junit完成

发布于 2025-02-12 16:10:31 字数 576 浏览 1 评论 0原文

我正在使用春季集成和Junit。

@Test
public void testOnePojo() throws Exception {
    ExecutorChannel orderSendChannel = 
    context.getBean("validationChannel", ExecutorChannel.class);
    ExecutorChannel orderReceiveChannel = context.getBean("auditChannel", ExecutorChannel.class);
    orderReceiveChannel.subscribe(t -> {
         System.out.println(t);//I want to see this output     
    }); 
    orderSendChannel.send(getMessageMessage());
}

我看不到接收通道的输出。 Junit订阅后退出。它有一种适当的方式在内部等待testOnePojo,直到auditchannel接收响应。

I am using spring integration and junit.

@Test
public void testOnePojo() throws Exception {
    ExecutorChannel orderSendChannel = 
    context.getBean("validationChannel", ExecutorChannel.class);
    ExecutorChannel orderReceiveChannel = context.getBean("auditChannel", ExecutorChannel.class);
    orderReceiveChannel.subscribe(t -> {
         System.out.println(t);//I want to see this output     
    }); 
    orderSendChannel.send(getMessageMessage());
}

I cannot see the output from the receiving channel. JUnit exits after subscribing. It there a proper way to wait inside testOnePojo until auditChannel receives a response.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界和平 2025-02-19 16:10:31

您可以使用

@Test
public void testOnePojo() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);

    ExecutorChannel orderSendChannel =
        context.getBean("validationChannel", ExecutorChannel.class);
    ExecutorChannel orderReceiveChannel = context.getBean("auditChannel", ExecutorChannel.class);
    orderReceiveChannel.subscribe(t -> {
        System.out.println(t);//I want to see this output
        countDownLatch.countDown();
    });
    orderSendChannel.send(getMessageMessage());

    countDownLatch.await();
}

you can use a CoundDownLatch in your test and wait for the MessageHandler to handle your message. Your example would look something like this:

@Test
public void testOnePojo() throws Exception {
    final CountDownLatch countDownLatch = new CountDownLatch(1);

    ExecutorChannel orderSendChannel =
        context.getBean("validationChannel", ExecutorChannel.class);
    ExecutorChannel orderReceiveChannel = context.getBean("auditChannel", ExecutorChannel.class);
    orderReceiveChannel.subscribe(t -> {
        System.out.println(t);//I want to see this output
        countDownLatch.countDown();
    });
    orderSendChannel.send(getMessageMessage());

    countDownLatch.await();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文