Add Spring JMS testing examples. (#12518)
This commit is contained in:
parent
ba18865ff3
commit
b055a790be
|
@ -41,6 +41,25 @@
|
|||
<version>${spring-boot-test.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>4.6.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.activemq.tooling</groupId>
|
||||
<artifactId>activemq-junit</artifactId>
|
||||
<version>5.16.5</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.testcontainers</groupId>
|
||||
<artifactId>testcontainers</artifactId>
|
||||
<version>1.17.3</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.spring.jms.testing;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan
|
||||
public class JmsApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ApplicationContext context = new AnnotationConfigApplicationContext(JmsApplication.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.spring.jms.testing;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jms.annotation.EnableJms;
|
||||
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
|
||||
import org.springframework.jms.config.JmsListenerContainerFactory;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
|
||||
@Configuration
|
||||
@EnableJms
|
||||
public class JmsConfig {
|
||||
|
||||
@Bean
|
||||
public JmsListenerContainerFactory<?> jmsListenerContainerFactory() {
|
||||
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
|
||||
factory.setConnectionFactory(connectionFactory());
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
return new ActiveMQConnectionFactory("tcp://localhost:61616");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JmsTemplate jmsTemplate() {
|
||||
return new JmsTemplate(connectionFactory());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.spring.jms.testing;
|
||||
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.jms.annotation.JmsListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MessageListener {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessageListener.class);
|
||||
|
||||
@JmsListener(destination = "queue-1")
|
||||
public void sampleJmsListenerMethod(TextMessage message) throws JMSException {
|
||||
logger.info("JMS listener received text message: {}", message.getText());
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
package com.baeldung.spring.jms.testing;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MessageSender {
|
||||
|
||||
@Autowired
|
||||
private JmsTemplate jmsTemplate;
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MessageSender.class);
|
||||
|
||||
public void sendTextMessage(String destination, String message) {
|
||||
logger.info("Sending message to {} destination with text {}", destination, message);
|
||||
jmsTemplate.send(destination, s -> s.createTextMessage(message));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
package com.baeldung.spring.jms.testing;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.apache.activemq.junit.EmbeddedActiveMQBroker;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jms.annotation.EnableJms;
|
||||
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
|
||||
import org.springframework.jms.config.JmsListenerContainerFactory;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.spring.jms.testing.EmbeddedActiveMqIntegrationTest.TestConfiguration;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { TestConfiguration.class })
|
||||
public class EmbeddedActiveMqIntegrationTest {
|
||||
|
||||
@ClassRule
|
||||
public static EmbeddedActiveMQBroker embeddedBroker = new EmbeddedActiveMQBroker();
|
||||
|
||||
@SpyBean
|
||||
private MessageListener messageListener;
|
||||
|
||||
@SpyBean
|
||||
private MessageSender messageSender;
|
||||
|
||||
@Test
|
||||
public void whenListening_thenReceivingCorrectMessage() throws JMSException {
|
||||
String queueName = "queue-1";
|
||||
String messageText = "Test message";
|
||||
|
||||
embeddedBroker.pushMessage(queueName, messageText);
|
||||
assertEquals(1, embeddedBroker.getMessageCount(queueName));
|
||||
|
||||
ArgumentCaptor<TextMessage> messageCaptor = ArgumentCaptor.forClass(TextMessage.class);
|
||||
|
||||
Mockito.verify(messageListener, Mockito.timeout(100))
|
||||
.sampleJmsListenerMethod(messageCaptor.capture());
|
||||
|
||||
TextMessage receivedMessage = messageCaptor.getValue();
|
||||
assertEquals(messageText, receivedMessage.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendingMessage_thenCorrectQueueAndMessageText() throws JMSException {
|
||||
String queueName = "queue-2";
|
||||
String messageText = "Test message";
|
||||
|
||||
messageSender.sendTextMessage(queueName, messageText);
|
||||
|
||||
assertEquals(1, embeddedBroker.getMessageCount(queueName));
|
||||
TextMessage sentMessage = embeddedBroker.peekTextMessage(queueName);
|
||||
assertEquals(messageText, sentMessage.getText());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableJms
|
||||
static class TestConfiguration {
|
||||
@Bean
|
||||
public JmsListenerContainerFactory<?> jmsListenerContainerFactory() {
|
||||
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
|
||||
factory.setConnectionFactory(connectionFactory());
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
return new ActiveMQConnectionFactory(embeddedBroker.getVmURL());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JmsTemplate jmsTemplate() {
|
||||
return new JmsTemplate(connectionFactory());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,101 @@
|
|||
package com.baeldung.spring.jms.testing;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import javax.jms.ConnectionFactory;
|
||||
import javax.jms.JMSException;
|
||||
import javax.jms.Message;
|
||||
import javax.jms.TextMessage;
|
||||
|
||||
import org.apache.activemq.ActiveMQConnectionFactory;
|
||||
import org.assertj.core.api.Assertions;
|
||||
import org.junit.ClassRule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.mock.mockito.SpyBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jms.annotation.EnableJms;
|
||||
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
|
||||
import org.springframework.jms.config.JmsListenerContainerFactory;
|
||||
import org.springframework.jms.core.JmsTemplate;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import com.baeldung.spring.jms.testing.TestContainersActiveMqIntegrationTest.TestConfiguration;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = { TestConfiguration.class, MessageSender.class })
|
||||
public class TestContainersActiveMqIntegrationTest {
|
||||
|
||||
@ClassRule
|
||||
public static GenericContainer<?> activeMqContainer = new GenericContainer<>(DockerImageName.parse("rmohr/activemq:5.14.3")).withExposedPorts(61616);
|
||||
|
||||
@SpyBean
|
||||
private MessageListener messageListener;
|
||||
|
||||
@Autowired
|
||||
private MessageSender messageSender;
|
||||
|
||||
@Autowired
|
||||
private JmsTemplate jmsTemplate;
|
||||
|
||||
@Test
|
||||
public void whenListening_thenReceivingCorrectMessage() throws JMSException {
|
||||
String queueName = "queue-1";
|
||||
String messageText = "Test message";
|
||||
|
||||
jmsTemplate.send(queueName, s -> s.createTextMessage(messageText));
|
||||
|
||||
ArgumentCaptor<TextMessage> messageCaptor = ArgumentCaptor.forClass(TextMessage.class);
|
||||
|
||||
Mockito.verify(messageListener, Mockito.timeout(100))
|
||||
.sampleJmsListenerMethod(messageCaptor.capture());
|
||||
|
||||
TextMessage receivedMessage = messageCaptor.getValue();
|
||||
assertEquals(messageText, receivedMessage.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSendingMessage_thenCorrectQueueAndMessageText() throws JMSException {
|
||||
String queueName = "queue-2";
|
||||
String messageText = "Test message";
|
||||
|
||||
messageSender.sendTextMessage(queueName, messageText);
|
||||
|
||||
Message sentMessage = jmsTemplate.receive(queueName);
|
||||
Assertions.assertThat(sentMessage)
|
||||
.isInstanceOf(TextMessage.class);
|
||||
|
||||
assertEquals(messageText, ((TextMessage) sentMessage).getText());
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableJms
|
||||
static class TestConfiguration {
|
||||
@Bean
|
||||
public JmsListenerContainerFactory<?> jmsListenerContainerFactory() {
|
||||
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
|
||||
factory.setConnectionFactory(connectionFactory());
|
||||
return factory;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ConnectionFactory connectionFactory() {
|
||||
String brokerUrlFormat = "tcp://%s:%d";
|
||||
String brokerUrl = String.format(brokerUrlFormat, activeMqContainer.getHost(), activeMqContainer.getFirstMappedPort());
|
||||
return new ActiveMQConnectionFactory(brokerUrl);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public JmsTemplate jmsTemplate() {
|
||||
return new JmsTemplate(connectionFactory());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue