编写测试以验证在JMS侦听器中收到的味精(Spring-Boot) [英] Writing tests to verify received msg in jms listener (Spring-Boot)

查看:116
本文介绍了编写测试以验证在JMS侦听器中收到的味精(Spring-Boot)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为以下内容编写测试;

I want to write test for something like below;

  1. src/main中有一个名为state-info-1的侦听器.

  1. There is a listener called state-info-1 in src/main.

它会对收到的任何消息进行一些更改,并在activemq主题state-info-2上发布新消息.

It does some changes to any message it gets and publishes the new message on activemq topic state-info-2.

我将构建一条虚拟消息并将其发布到activemq主题state-info-1.

I will build a dummy message and publish on to activemq topic state-info-1.

最后确认,收到的关于主题state-info-2的消息是否与我预期的一样.

Finally verify that, the received message on topic state-info-2 is like i expected.

我的听众就像;

@JmsListener(destination = "state-info-1", containerFactory = "connFactory")
public void receiveMessage(Message payload) {
    // Do Stuff and Publish to state-info-2
}

是否可以为此编写测试?还是我必须以其他方式做到?

Is it possible i can write test for this? Or i have to do it in some other way?

此外,我看着这个:但这不是我所期望的.

But this is not what i am expecting.

任何帮助或向正确的方向推就足够了.

Any help or push in the right direction will be enough.

谢谢您的时间.

推荐答案

@SpringBootApplication
public class So42803627Application {

    public static void main(String[] args) {
        SpringApplication.run(So42803627Application.class, args);
    }

    @Autowired
    private JmsTemplate jmsTemplate;

    @JmsListener(destination = "foo")
    public void handle(String in) {
        this.jmsTemplate.convertAndSend("bar", in.toUpperCase());
    }

}

@RunWith(SpringRunner.class)
@SpringBootTest
public class So42803627ApplicationTests {

    @Autowired
    private JmsTemplate jmsTemplate;

    @Test
    public void test() {
        this.jmsTemplate.convertAndSend("foo", "Hello, world!");
        this.jmsTemplate.setReceiveTimeout(10_000);
        assertThat(this.jmsTemplate.receiveAndConvert("bar")).isEqualTo("HELLO, WORLD!");
    }

}

这篇关于编写测试以验证在JMS侦听器中收到的味精(Spring-Boot)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆