How to test if channel and queue are created and bind to the exchange in rabbitMq using rabbitMq TestContainer in Micronaut

1k views Asked by At

At the start of the application, I am creating the below channel and associated queues

@Singleton
public class ChannelPoolListener extends ChannelInitializer {

    @Override
    public void initialize(Channel channel) throws IOException {
        channel.exchangeDeclare("micronaut", BuiltinExchangeType.DIRECT, true); 

        channel.queueDeclare("inventory", true, false, false, null); 
        channel.queueBind("inventory", "micronaut", "books.inventory"); 

        channel.queueDeclare("catalogue", true, false, false, null); 
        channel.queueBind("catalogue", "micronaut", "books.catalogue"); 
    }
}

I want to write the JUnit 5 test to check if queues are created and bind to the exchange using the rabbitMq Test container.

From the RabbitMq java API, I know we have a method for the channel. But not sure how can I inject the Channel in JUnit 5

GetResponse response = rabbitChannel.basicGet(QUEUE_NAME, BOOLEAN_NOACK);
2

There are 2 answers

1
James Kleeh On

Just use the queue with a test client/subscriber via the Micronaut annotations or the java client API directly.

0
San Jaisy On

Anyone looking for this kind of result

@BeforeAll
    @DisplayName("Initial setup")
    void initialSetup() {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            new ChannelPoolListener().initialize(channel);
            AMQP.Queue.DeclareOk response = channel.queueDeclarePassive("Hello world");
            Assertions.assertTrue(application.isRunning());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }