Back to Repositories

Testing RocketMQ Producer Messaging Patterns in SpringBoot-Labs

This test suite validates RocketMQ message production functionality in a Spring Boot application, focusing on synchronous, asynchronous, and one-way message sending patterns. It demonstrates comprehensive producer testing with various delivery mechanisms using the RocketMQ ONS client.

Test Coverage Overview

The test suite covers three essential message sending patterns in RocketMQ:
  • Synchronous message sending with result verification
  • Asynchronous message sending with callback handling
  • One-way message sending for fire-and-forget scenarios
Each test case validates the producer’s ability to handle different message delivery mechanisms while ensuring proper message consumption through countdown latches.

Implementation Analysis

The testing approach utilizes Spring Boot’s test framework with JUnit4 integration. It implements a producer-focused testing strategy using @SpringBootTest for full context loading and dependency injection. The tests employ CountDownLatch for synchronization and proper message consumption verification.

Each test method follows a consistent pattern of message generation, sending, and result validation, with appropriate logging for tracking test execution.

Technical Details

  • Spring Boot Test framework with JUnit4 runner
  • RocketMQ ONS client integration
  • SLF4J logging implementation
  • Automated message ID generation using system time
  • Synchronization using CountDownLatch
  • SendCallback interface implementation for async testing

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, comprehensive logging, and robust error handling. It demonstrates effective use of dependency injection, appropriate test method naming, and proper handling of asynchronous operations.

The code organization follows a clear pattern with distinct test methods for each sending mechanism, making the test suite maintainable and easily extensible.

yudaocode/springboot-labs

lab-31/lab-31-rocketmq-ons/src/test/java/cn/iocoder/springboot/lab31/rocketmqdemo/producer/Demo01ProducerTest.java

            
package cn.iocoder.springboot.lab31.rocketmqdemo.producer;

import cn.iocoder.springboot.lab31.rocketmqdemo.Application;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
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 Demo01ProducerTest {

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

    @Autowired
    private Demo01Producer producer;

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

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

    @Test
    public void testASyncSend() throws InterruptedException {
        int id = (int) (System.currentTimeMillis() / 1000);
        producer.asyncSend(id, new SendCallback() {

            @Override
            public void onSuccess(SendResult result) {
                logger.info("[testASyncSend][发送编号:[{}] 发送成功,结果为:[{}]]", id, result);
            }

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

        });

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

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

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

}