Class WithRabbitMock

java.lang.Object
fr.ght1pc9kc.testy.beat.extensions.WithRabbitMock
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver

public final class WithRabbitMock extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
Allow getting a rabbit broker in tests.
  • Can be configured with a customized ObjectMapper
  • Starts an embedded AMQP broker
  • Opens an AMQP connection and channel on each test (closes after)
  • Builds sender and receiver options, injectable as test parameters
  • Can declare many queues with related exchanges
  • Builds a MockedSender and a MockedReceiver to simplify the mocking of the queues.

Usage :
     private static final String QUEUE_1 = "test-queue-1";
     private static final String QUEUE_2 = "test-queue-2";
     private static final String EXCHANGE_1 = "test-exchange-1";
     private static final String EXCHANGE_2 = "test-exchange-2";
  
     private static final WithObjectMapper withObjectMapper = WithObjectMapper.builder()
             .addModule(new com.fasterxml.jackson.module.paramnames.ParameterNamesModule())
             .build();
     private static final WithRabbitMock withRabbit = WithRabbitMock.builder()
             .declareQueueAndExchange(QUEUE_1, EXCHANGE_1)
             .declareQueueAndExchange(QUEUE_2, EXCHANGE_2)
             .build();
     @RegisterExtension
     @SuppressWarnings("unused")
     static final ChainedExtension chain = ChainedExtension.outer(withObjectMapper)
             .append(withRabbit)
             .register();
 

Example to test a "listener" class. A "listener" is expected to:
  • Declare the queue and exchange
  • Consume the messages from the queue
  • Apply a treatment on the message (specific to each listener)
  • Reply another message on the reply queue
  • The MockedSender can be used to simplify the sending of messages on a queue.

     @Test
     void should_consume_queue_and_reply_message(MockedSender mockedSender, ObjectMapper objectMapper) {
          // Here the tested listener creates a queue and consumes on it.
          tested.subscribe();
  
          final String request = "message sent to tested";
          final byte[] requestBody = DeliveryMappingHelper.writeObjectAsByte(request, objectMapper);
          final String actualResponse = mockedSender.rpc(AmqpMessage.of(requestBody))
                     .on("exchange", "routing-key")
                     .map(delivery -> DeliveryMappingHelper.readDeliveryValue(delivery, objectMapper, String.class))
                     .block();
          assertThat(actualResponse).isEqualTo("expected message replied by tested listener");
      }
 

Assert example to test an "emitter" class. An "emitter" is expected to:
  • Send a message on the queue/exchange
  • Treat the response.

Note that:
     @Test
     void should_emit_message_and_manage_response(MockedReceiver mockedReceiver,
                                                  ObjectMapper objectMapper) throws IOException {
         final String response = "response from receiver";
         final byte[] responseBody = DeliveryMappingHelper.writeObjectAsByte(response, objectMapper);
         final Flux<Delivery> receivedMessages = receiver.consumeOne()
                  .on("queue")
                  .thenRespond(AmqpMessage.of(responseBody))
                  .start();
  
         // Tested method sending a message on the queue
         tested.execute();
  
         final List<String> actualEmittedMessages = receivedMessages
                 .map(delivery -> DeliveryMappingHelper.readDeliveryValue(delivery, objectMapper, String.class))
                 .collect(Collectors.toList());
         assertThat(actualEmittedMessages).containsExactly("message sent by tested");
     }
 
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Allow to build a Channel rabbit
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    void
    afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
     
     
    com.rabbitmq.client.Channel
    getRabbitChannel(org.junit.jupiter.api.extension.ExtensionContext context)
    Get the Rabbit Channel used for communication
    com.rabbitmq.client.Connection
    getRabbitConnection(org.junit.jupiter.api.extension.ExtensionContext context)
    Get the Rabbit connection used for communication
    reactor.rabbitmq.ReceiverOptions
    getReceiverOptions(org.junit.jupiter.api.extension.ExtensionContext context)
    Get the Receiver Options used for communication
    reactor.rabbitmq.SenderOptions
    getSenderOptions(org.junit.jupiter.api.extension.ExtensionContext context)
    Get the Sender Options used for communication
    resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    boolean
    supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • builder

      public static WithRabbitMock.WithRabbitMockBuilder builder()
    • beforeAll

      public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeAll in interface org.junit.jupiter.api.extension.BeforeAllCallback
    • afterAll

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) throws IOException
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
      Throws:
      IOException
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
      Throws:
      Exception
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • getRabbitChannel

      public com.rabbitmq.client.Channel getRabbitChannel(org.junit.jupiter.api.extension.ExtensionContext context)
      Get the Rabbit Channel used for communication
      Parameters:
      context - The extension context useful for retrieving object into store
      Returns:
      The Channel created
    • getRabbitConnection

      public com.rabbitmq.client.Connection getRabbitConnection(org.junit.jupiter.api.extension.ExtensionContext context)
      Get the Rabbit connection used for communication
      Parameters:
      context - The extension context useful for retrieving object into store
      Returns:
      The connection.
    • getSenderOptions

      public reactor.rabbitmq.SenderOptions getSenderOptions(org.junit.jupiter.api.extension.ExtensionContext context)
      Get the Sender Options used for communication
      Parameters:
      context - The extension context useful for retrieving object into store
      Returns:
      The Sender Options used for channel creation
    • getReceiverOptions

      public reactor.rabbitmq.ReceiverOptions getReceiverOptions(org.junit.jupiter.api.extension.ExtensionContext context)
      Get the Receiver Options used for communication
      Parameters:
      context - The extension context useful for retrieving object into store
      Returns:
      The Receiver Options used for channel creation