等待执行通道在junit中完成

yptwkmov  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(114)

我使用的是Spring集成和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收到响应。

jvlzgdj9

jvlzgdj91#

您可以在测试中使用CoundDownLatch并等待MessageHandler处理您的消息。您的示例如下所示:

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

相关问题