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
Implementation Analysis
Technical Details
Best Practices Demonstrated
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();
}
}