Back to Repositories

Testing ActiveMQ Message Producer Integration in SpringBoot-Labs

This test suite validates ActiveMQ message producer functionality in a Spring Boot application, covering both synchronous and asynchronous message sending capabilities. The tests ensure reliable message delivery and proper handling of success/failure scenarios.

Test Coverage Overview

The test suite provides comprehensive coverage of ActiveMQ producer operations:
  • Synchronous message sending with confirmation
  • Asynchronous message sending with callback handling
  • Message ID generation and tracking
  • Error handling and success validation

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit integration. It employs CountDownLatch for thread synchronization and ListenableFutureCallback for asynchronous operation handling. The producer tests are implemented using Spring’s dependency injection and test context framework.

Technical Details

Key technical components include:
  • SpringRunner for test execution
  • SpringBootTest annotation for application context
  • SLF4J logging for test verification
  • CountDownLatch for thread coordination
  • ListenableFutureCallback for async response handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Proper test isolation using Spring’s test context
  • Comprehensive logging for debugging and verification
  • Appropriate handling of async operations
  • Clean separation of sync and async test cases
  • Effective use of Spring Boot testing annotations

yudaocode/springboot-labs

lab-32/lab-32-activemq-demo/src/test/java/cn/iocoder/springboot/lab32/activemqdemo/producer/Demo01ProducerTest.java

            
package cn.iocoder.springboot.lab32.activemqdemo.producer;

import cn.iocoder.springboot.lab32.activemqdemo.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.concurrent.ListenableFutureCallback;

import java.util.concurrent.CountDownLatch;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class Demo01ProducerTest {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private Demo01Producer producer;

    @Test
    public void testSyncSend() throws InterruptedException {
        // 发送消息
        int id = (int) (System.currentTimeMillis() / 1000);
        producer.syncSend(id);
        logger.info("[testSyncSend][发送编号:[{}] 发送成功]", id);

        // 阻塞等待,保证消费
        new CountDownLatch(1).await();
    }

    @Test
    public void testAsyncSend() throws InterruptedException {
        int id = (int) (System.currentTimeMillis() / 1000);
        producer.asyncSend(id).addCallback(new ListenableFutureCallback<Void>() {

            @Override
            public void onFailure(Throwable e) {
                logger.info("[testASyncSend][发送编号:[{}] 发送异常]]", id, e);
            }

            @Override
            public void onSuccess(Void aVoid) {
                logger.info("[testASyncSend][发送编号:[{}] 发送成功,发送成功]", id);
            }

        });
        logger.info("[testASyncSend][发送编号:[{}] 调用完成]", id);

        // 阻塞等待,保证消费
        new CountDownLatch(1).await();
    }

}