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,org.junit.jupiter.api.extension.TestInstantiationAwareExtension
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
MockedSenderand aMockedReceiverto 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
MockedSendercan 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
MockedReceivercan 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 classAllow to build a Channel rabbitNested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope -
Method Summary
Modifier and TypeMethodDescriptionvoidafterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) voidafterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext) voidbeforeAll(org.junit.jupiter.api.extension.ExtensionContext context) voidbeforeEach(org.junit.jupiter.api.extension.ExtensionContext context) builder()com.rabbitmq.client.ChannelgetRabbitChannel(org.junit.jupiter.api.extension.ExtensionContext context) Get the Rabbit Channel used for communicationcom.rabbitmq.client.ConnectiongetRabbitConnection(org.junit.jupiter.api.extension.ExtensionContext context) Get the Rabbit connection used for communicationreactor.rabbitmq.ReceiverOptionsgetReceiverOptions(org.junit.jupiter.api.extension.ExtensionContext context) Get the Receiver Options used for communicationreactor.rabbitmq.SenderOptionsgetSenderOptions(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) booleansupportsParameter(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, waitMethods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Method Details
-
builder
-
beforeAll
public void beforeAll(org.junit.jupiter.api.extension.ExtensionContext context) - Specified by:
beforeAllin interfaceorg.junit.jupiter.api.extension.BeforeAllCallback
-
afterAll
public void afterAll(org.junit.jupiter.api.extension.ExtensionContext extensionContext) - Specified by:
afterAllin interfaceorg.junit.jupiter.api.extension.AfterAllCallback
-
beforeEach
- Specified by:
beforeEachin interfaceorg.junit.jupiter.api.extension.BeforeEachCallback- Throws:
IOException
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws Exception - Specified by:
afterEachin 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:
supportsParameterin 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:
resolveParameterin 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
-