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 @ 2021-03-31 15:05  刘宏缔的架构森林  阅读(1398)  评论(0编辑  收藏  举报