0

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);
San Jaisy
  • 15,327
  • 34
  • 171
  • 290
  • "I want to write the JUnit 5 test to check if queues are created and bind to the exchange using the rabbitMq Test container" - Are you wanting to write a test that verifies those things in particular, or are you wanting your test to support your code that relies on those things? – Jeff Scott Brown Aug 16 '21 at 17:18
  • @JeffScottBrown I am wanting to write a test that verifies those things in particular. For instance, the rabbitMq must contain the queue inventory and catalogue – San Jaisy Aug 17 '21 at 00:55

2 Answers2

0

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

James Kleeh
  • 12,094
  • 5
  • 34
  • 61
0

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();
        }
    }
San Jaisy
  • 15,327
  • 34
  • 171
  • 290