Back to Repositories

Testing RabbitMQ Message Producer Synchronization in SpringBoot-Labs

This test suite examines RabbitMQ message producer functionality in a Spring Boot application, focusing on synchronous message sending scenarios. The tests verify both successful message delivery and failure handling with custom headers and error conditions.

Test Coverage Overview

The test suite provides coverage for synchronous RabbitMQ message production scenarios:

  • Successful message sending with valid headers
  • Failure scenarios with invalid header values
  • Message ID generation and tracking
  • Producer-side validation and error handling

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit integration. It implements synchronous producer testing patterns using CountDownLatch for ensuring message consumption completion, while leveraging Spring’s dependency injection for producer instantiation.

The tests demonstrate both positive and negative scenarios using timestamp-based message IDs and header value validation.

Technical Details

  • Spring Boot Test framework with @SpringBootTest annotation
  • JUnit 4 test runner with SpringRunner
  • SLF4J logging integration
  • CountDownLatch for synchronization control
  • Custom message types (Demo04Message)
  • Autowired producer injection

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, meaningful test naming, and comprehensive error scenario coverage. It demonstrates effective use of Spring Boot’s testing infrastructure, proper resource management, and synchronization handling for asynchronous operations.

  • Clear test method naming conventions
  • Proper logging implementation
  • Effective use of test annotations
  • Robust synchronization handling

yudaocode/springboot-labs

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

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

import cn.iocoder.springboot.lab04.rabbitmqdemo.Application;
import cn.iocoder.springboot.lab04.rabbitmqdemo.message.Demo04Message;
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 java.util.concurrent.CountDownLatch;

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

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

    @Autowired
    private Demo04Producer producer;

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

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

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

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

}