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.
Usage :
Example to test a "listener" class. A "listener" is expected to:
Assert example to test an "emitter" class. An "emitter" is expected to:
Note that:
- 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 aMockedReceiver
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:
- An
MockedReceiver
can be injected to the test. - It can consume a defined number of messages on a queue and reply defined responses.
- The method
MockedReceiver.MockedConsumerBuilder.start()
returns all the requests consumed from the queue.
@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 ClassesModifier and TypeClassDescriptionstatic class
Allow to build a Channel rabbit -
Method Summary
Modifier and TypeMethodDescriptionvoid
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) builder()
com.rabbitmq.client.Channel
getRabbitChannel
(org.junit.jupiter.api.extension.ExtensionContext context) Get the Rabbit Channel used for communicationcom.rabbitmq.client.Connection
getRabbitConnection
(org.junit.jupiter.api.extension.ExtensionContext context) Get the Rabbit connection used for communicationreactor.rabbitmq.ReceiverOptions
getReceiverOptions
(org.junit.jupiter.api.extension.ExtensionContext context) Get the Receiver Options used for communicationreactor.rabbitmq.SenderOptions
getSenderOptions
(org.junit.jupiter.api.extension.ExtensionContext context) Get the Sender Options used for communicationresolveParameter
(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)
-
Method Details
-
builder
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeAll
in interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) - Specified by:
afterAll
in interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
beforeEach
- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Throws:
IOException
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception - Specified by:
afterEach
in interfaceorg.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 interfaceorg.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 interfaceorg.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
-