spring-boot - 编写测试来验证 jms 监听器中收到的消息(Spring-Boot)

标签 spring-boot jms activemq spring-jms spring-test

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

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

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

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

  4. 最后验证一下,收到的有关主题 state-info-2 的消息是否符合我的预期。

我的听众就像;

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

我可以为此编写测试吗?或者我必须以其他方式做到这一点?

另外,我看了这个:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java

但这不是我所期待的。

任何帮助或插入正确的方向就足够了。

感谢您的宝贵时间。

最佳答案

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

}

关于spring-boot - 编写测试来验证 jms 监听器中收到的消息(Spring-Boot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42803627/

相关文章:

spring-boot - Spring Boot 和 Kafka : Failed to send SSL Close message

java - 如何协调 JMS 浏览器和消费者?

java - JMS 和 JPA - 事务服务最佳实践

java - 有没有办法将 2 个数据库字段映射到 JPA 中的自定义对象?

java - 如何在 Spring Boot 的路径变量中传递本地日期?

java - 需要有关 Java JMS ResourceAllocationException 的帮助

由于地址已在使用错误,Activemq 无法运行

Java 生产者、Stompy Python 消费者、ActiveMQ

java - ActiveMQ 消费者 OutOfMemoryException

java - 飞行路线迁移: Data Import With German Umlauts