Back to Repositories

Testing RabbitMQ Producer Implementation in SpringBoot-Labs

This test suite validates RabbitMQ message producer functionality in a Spring Boot application, focusing on synchronous and asynchronous message sending patterns. The tests verify different message publishing scenarios and error handling mechanisms.

Test Coverage Overview

The test suite provides comprehensive coverage of RabbitMQ producer operations:
  • Synchronous message sending with custom and default routing
  • Asynchronous message sending with callback handling
  • High-volume message publishing scenarios
  • Error handling and success confirmation flows

Implementation Analysis

The testing approach utilizes Spring Boot’s testing framework with JUnit4 integration.

Key patterns include:
  • CountDownLatch for asynchronous operation handling
  • ListenableFutureCallback for async response processing
  • Timestamp-based message ID generation
  • SLF4J logging for operation tracking

Technical Details

Testing infrastructure includes:
  • @SpringBootTest for application context loading
  • SpringRunner for test execution
  • Autowired producer injection
  • SLF4J logging framework
  • CountDownLatch for thread synchronization

Best Practices Demonstrated

The test suite exemplifies several testing best practices:
  • Isolated test methods for different sending patterns
  • Proper resource cleanup and thread management
  • Comprehensive logging for debugging
  • Clear test method naming conventions
  • Error handling verification

yudaocode/springboot-labs

lab-04-rabbitmq/lab-04-rabbitmq-demo/src/test/java/cn/iocoder/springboot/lab04/rabbitmqdemo/producer/Demo01ProducerTest.java

            
package cn.iocoder.springboot.lab04.rabbitmqdemo.producer;

import cn.iocoder.springboot.lab04.rabbitmqdemo.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 tesSyncSendDefault() throws InterruptedException {
        int id = (int) (System.currentTimeMillis() / 1000);
        producer.syncSendDefault(id);
        logger.info("[tesSyncSendDefault][发送编号:[{}] 发送成功]", 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();
    }

    @Test
    public void nothing() throws InterruptedException {
        // 阻塞等待,保证消费
        new CountDownLatch(1).await();
    }

    @Test
    public void nothing02() throws InterruptedException {
        for (int i = 0; i < 1000; i++) {
            int id = (int) (System.currentTimeMillis() / 1000);
            producer.syncSend(id);
            logger.info("[testSyncSend][发送编号:[{}] 发送成功]", id);
            Thread.sleep(5000L);
        }

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

}