I need to mock a RabbitMQ in my unit Test

后端 未结 3 2126
-上瘾入骨i
-上瘾入骨i 2021-02-07 19:17

I am using a RabbitMQ in my project.

I have in my consumer the code of the client part of rabbitMQ and the connection need a tls1.1 to connect with the real MQ.

3条回答
  •  南旧
    南旧 (楼主)
    2021-02-07 20:17

    As I understand it, there are two things trying to be tested in the question:

    • TLS configuration to connect to RabbitMQ
    • basicPublish / basicConsume (what's called delivery) behavior regarding interactions with the rest of the application

    For the first one, as TLS itself is being tested, only connecting to a real instance of RabbitMQ with correct truststore configured will prove that configuration is working

    For the second one however, for tests demonstrating features of the app (with tools like Cucumber for readability), you may try a library i'm working on: rabbitmq-mock (and that's why I'm digging up an old post)

    Just include it as dependency:

    
        com.github.fridujo
        rabbitmq-mock
        1.0.14
        test
    
    

    And replace new ConnectionFactory() by new MockConnectionFactory() in your unit test.

    Samples are available in the project: https://github.com/fridujo/rabbitmq-mock/blob/master/src/test/java/com/github/fridujo/rabbitmq/mock/IntegrationTest.java

提交回复
热议问题