Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.github.fridujo.rabbitmq.mock;
import static org.junit.jupiter.api.Assertions.*;

import com.github.fridujo.rabbitmq.mock.MockConnectionFactory;
import com.rabbitmq.client.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

class MessagePublishingTest {

private Connection connection;
private Channel channel;

@BeforeEach
void setUp() throws IOException, TimeoutException { // Create a mock of an in-memory RabbitMQ mock
ConnectionFactory factory = new MockConnectionFactory();
connection = factory.newConnection();
channel = connection.createChannel();
}

@Test
void testMessagePublishingToQueue() throws IOException {
String exchangeName = "testExchange";
String queueName = "testQueue";
String routingKey = "testKey";
String message = "Test message for RabbitMQ";

// Declare exchange and queue, then bind them
channel.exchangeDeclare(exchangeName, BuiltinExchangeType.DIRECT);
channel.queueDeclare(queueName, false, false, false, null);
channel.queueBind(queueName, exchangeName, routingKey);

// Publish a message
channel.basicPublish(exchangeName, routingKey, null, message.getBytes(StandardCharsets.UTF_8));

// Consume the message from the queue
GetResponse response = channel.basicGet(queueName, true);
assertNotNull(response);
assertEquals(message, new String(response.getBody(), StandardCharsets.UTF_8));
}
}