spring boot单元测试之十二:用RabbitMQ mock库做消息生产/消费的mock(spring boot 2.4.4)

一,演示项目的相关信息

1,地址:

https://github.com/liuhongdi/rabbitmock

2,功能说明:演示了用RabbitMQ mock库做消息生产/消费的mock

3,  项目结构:如图:

说明:刘宏缔的架构森林是一个专注架构的博客,

网站:https://blog.imgtouch.com
本文: https://blog.imgtouch.com/index.php/2023/05/27/spring-boot-dan-yuan-ce-shi-zhi-shi-er-yong-rabbitmq-mock/

         对应的源码可以访问这里获取: https://github.com/liuhongdi/

说明:作者:刘宏缔 邮箱: 371125307@qq.com

二,配置文件说明

1,pom.xml

复制代码
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.fridujo</groupId>
            <artifactId>rabbitmq-mock</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.amqp</groupId>
            <artifactId>spring-rabbit-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.4.4</version>
            <scope>compile</scope>
        </dependency>
复制代码

2,application-dev.yml

#注意host改成了一个不存在的ip

#rabbitmq
spring:
  rabbitmq:
    host: 1.1.1.1
    port: 5672
    username: root
    password: 123456

 

三,java代码说明

1,config/TopicConfig.java

复制代码
@Configuration
public class TopicConfig {
    public static final String TOPIC_QUEUE1 = "topic.queue1";
    public static final String TOPIC_QUEUE2 = "topic.queue2";
    public static final String TOPIC_EXCHANGE = "topic.exchange";

    @Profile("dev")
    @Bean
    ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory(MockConnectionFactoryFactory.build());
    }

    @Bean
    public Queue topicQueue1() {
        return new Queue(TOPIC_QUEUE1);
    }
    @Bean
    public Queue topicQueue2() {
        return new Queue(TOPIC_QUEUE2);
    }
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }
    @Bean
    public Binding topicBinding1() {
        return BindingBuilder.bind(topicQueue1()).to(topicExchange()).with("topic.messge");
    }
    @Bean
    public Binding topicBinding2() {
        //#表示0个或多个word
        return BindingBuilder.bind(topicQueue2()).to(topicExchange()).with("topic.#");
    }
}
复制代码

 

2,pojo/Goods.java

复制代码
public class Goods implements Serializable {
    
    private static final long serialVersionUID = 6629065135155452917L;

    private Long goodsId;
    private String goodsName;
    private Double goodsPrice;

    public Long getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(Long goodsId) {
        this.goodsId = goodsId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public Double getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(Double goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    public Goods(Long goodsId, String goodsName, Double goodsPrice) {
        this.goodsId = goodsId;
        this.goodsName = goodsName;
        this.goodsPrice = goodsPrice;
    }

    public Goods() {
    }

    @Override
    public String toString() {
        return "Goods{" +
                "goodsId=" + goodsId +
                ", goodsName='" + goodsName + '\'' +
                ", goodsPrice=" + goodsPrice +
                '}';
    }
}
复制代码

 

3,receiver/TopicReceiver.java

复制代码
@Component
public class TopicReceiver {
    @RabbitListener(queues = TopicConfig.TOPIC_QUEUE1)
    public void receiveTopic1(Goods goods) {
        System.out.println("receiveTopic1收到消息:" + goods.toString());
    }

    @RabbitListener(queues = TopicConfig.TOPIC_QUEUE2)
    public void receiveTopic2(Goods goods) {
        System.out.println("receiveTopic2收到消息:" + goods.toString());
    }
}
复制代码

 

4,sender/TopicSender.java

复制代码
@Component
public class TopicSender {
    //private static final Logger log = LoggerFactory.getLogger(TopicSender.class);
    @Autowired
    private AmqpTemplate amqpTemplate;

    public void sendTopicQueue() {
        Goods goods1 = new Goods(1L,"测试商品1",98.6);
        Goods goods2 = new Goods(2L,"测试商品2",100.0);
        System.out.println("TopicSender已发送消息");
        // 第一个参数:TopicExchange名字
        // 第二个参数:Route-Key
        // 第三个参数:要发送的内容
        this.amqpTemplate.convertAndSend(TopicConfig.TOPIC_EXCHANGE, "topic.messge", goods1 );
        this.amqpTemplate.convertAndSend(TopicConfig.TOPIC_EXCHANGE, "topic.messge2", goods2);
    }
}
复制代码

 

5,module/RabbitmqMockerTest.java

复制代码
@SpringBootTest
@ActiveProfiles("dev")
public class RabbitmqMockerTest { @Autowired AmqpTemplate amqpTemplate; @Autowired private TopicSender topicSender; @Test @DisplayName("测试发送消息") public void testSend() { topicSender.sendTopicQueue(); } @Test @DisplayName("测试接收消息") public void testReceive() { Goods goods3 = new Goods(3L,"测试商品3",198.6); this.amqpTemplate.convertAndSend(TopicConfig.TOPIC_EXCHANGE, "topic.messge", goods3 ); Goods goods = (Goods)amqpTemplate.receiveAndConvert("topic.queue1"); System.out.println("goods begin"); System.out.println(goods); assertThat(goods.getGoodsId(), equalTo(3L)); } }
复制代码

 

四,测试效果

五,查看spring boot版本:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.4)

 

posted @   刘宏缔的架构森林  阅读(1421)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示