好吧,我希望可以以某种方式挂接到消息驱动的Pojos生命周期中,以实现此目的,但是在解决有关异步代码的其他SO问题时,我想出了一个基于
CountDownLatch
- 所有工作完成后,带
@JMSListener
注释的方法应调用countDown()
CountDownLatch:
@JmsListener(destination = "dest", containerFactory = "cf") public void processMessage(TextMessage message) throws JMSException { //do the actual processing actualProcessing(message); //if there's countDownLatch call the countdown. if(countDownLatch != null) { countDownLatch.countDown(); } }- 在testMethod中
@Test public void test() throws InterruptedException { //initialize the countDownLatch and set in on the processing class CountDownLatch countDownLatch = new CountDownLatch(1); messageProcessor.setCountDownLatch(countDownLatch); //sendthemessage sendSomeMessage(); //wait for the processing method to call countdown() countDownLatch.await(); verify(); }该解决方案的缺点是您必须
@JMSListener针对集成测试专门更改带注释的方法



