Search in sources :

Example 31 with Session

use of jakarta.jms.Session in project spring-framework by spring-projects.

the class MessagingMessageListenerAdapterTests method replyPayloadNoDestination.

@Test
public void replyPayloadNoDestination() throws JMSException {
    Queue replyDestination = mock(Queue.class);
    Session session = mock(Session.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage responseMessage = mock(TextMessage.class);
    given(session.createTextMessage("Response")).willReturn(responseMessage);
    given(session.createProducer(replyDestination)).willReturn(messageProducer);
    MessagingMessageListenerAdapter listener = getPayloadInstance("Response", "replyPayloadNoDestination", Message.class);
    listener.setDefaultResponseDestination(replyDestination);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session, times(0)).createQueue(anyString());
    verify(session).createTextMessage("Response");
    verify(messageProducer).send(responseMessage);
    verify(messageProducer).close();
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) TextMessage(jakarta.jms.TextMessage) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 32 with Session

use of jakarta.jms.Session in project spring-framework by spring-projects.

the class MessagingMessageListenerAdapterTests method exceptionInListener.

@Test
public void exceptionInListener() {
    jakarta.jms.Message message = new StubTextMessage("foo");
    Session session = mock(Session.class);
    MessagingMessageListenerAdapter listener = getSimpleInstance("fail", String.class);
    assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() -> listener.onMessage(message, session)).havingCause().isExactlyInstanceOf(IllegalArgumentException.class).withMessage("Expected test exception");
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 33 with Session

use of jakarta.jms.Session in project spring-framework by spring-projects.

the class MessagingMessageListenerAdapterTests method exceptionInInvocation.

@Test
public void exceptionInInvocation() {
    jakarta.jms.Message message = new StubTextMessage("foo");
    Session session = mock(Session.class);
    MessagingMessageListenerAdapter listener = getSimpleInstance("wrongParam", Integer.class);
    assertThatExceptionOfType(ListenerExecutionFailedException.class).isThrownBy(() -> listener.onMessage(message, session)).withCauseExactlyInstanceOf(MessageConversionException.class);
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 34 with Session

use of jakarta.jms.Session in project spring-framework by spring-projects.

the class MessagingMessageListenerAdapterTests method replyUsesMessageConverterForPayload.

@Test
public void replyUsesMessageConverterForPayload() throws JMSException {
    Session session = mock(Session.class);
    MessageConverter messageConverter = mock(MessageConverter.class);
    given(messageConverter.toMessage("Response", session)).willReturn(new StubTextMessage("Response"));
    Message<String> result = MessageBuilder.withPayload("Response").build();
    MessagingMessageListenerAdapter listener = getSimpleInstance("echo", Message.class);
    listener.setMessageConverter(messageConverter);
    jakarta.jms.Message replyMessage = listener.buildMessage(session, result);
    verify(messageConverter, times(1)).toMessage("Response", session);
    assertThat(replyMessage).as("reply should never be null").isNotNull();
    assertThat(((TextMessage) replyMessage).getText()).isEqualTo("Response");
}
Also used : StubTextMessage(org.springframework.jms.StubTextMessage) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MappingJackson2MessageConverter(org.springframework.jms.support.converter.MappingJackson2MessageConverter) MessageConverter(org.springframework.jms.support.converter.MessageConverter) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session) Test(org.junit.jupiter.api.Test)

Example 35 with Session

use of jakarta.jms.Session in project spring-framework by spring-projects.

the class MessagingMessageListenerAdapterTests method testReplyWithJackson.

public TextMessage testReplyWithJackson(String methodName, String replyContent) throws JMSException {
    Queue replyDestination = mock(Queue.class);
    Session session = mock(Session.class);
    MessageProducer messageProducer = mock(MessageProducer.class);
    TextMessage responseMessage = mock(TextMessage.class);
    given(session.createTextMessage(replyContent)).willReturn(responseMessage);
    given(session.createProducer(replyDestination)).willReturn(messageProducer);
    MessagingMessageListenerAdapter listener = getPayloadInstance("Response", methodName, Message.class);
    MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();
    messageConverter.setTargetType(MessageType.TEXT);
    listener.setMessageConverter(messageConverter);
    listener.setDefaultResponseDestination(replyDestination);
    listener.onMessage(mock(jakarta.jms.Message.class), session);
    verify(session, times(0)).createQueue(anyString());
    verify(session).createTextMessage(replyContent);
    verify(messageProducer).send(responseMessage);
    verify(messageProducer).close();
    return responseMessage;
}
Also used : MappingJackson2MessageConverter(org.springframework.jms.support.converter.MappingJackson2MessageConverter) StubTextMessage(org.springframework.jms.StubTextMessage) Message(org.springframework.messaging.Message) TextMessage(jakarta.jms.TextMessage) MessageProducer(jakarta.jms.MessageProducer) Queue(jakarta.jms.Queue) StubTextMessage(org.springframework.jms.StubTextMessage) TextMessage(jakarta.jms.TextMessage) Session(jakarta.jms.Session)

Aggregations

Session (jakarta.jms.Session)84 Test (org.junit.jupiter.api.Test)71 Connection (jakarta.jms.Connection)30 Message (jakarta.jms.Message)27 TextMessage (jakarta.jms.TextMessage)26 StubTextMessage (org.springframework.jms.StubTextMessage)22 ConnectionFactory (jakarta.jms.ConnectionFactory)21 JMSException (jakarta.jms.JMSException)20 MessageProducer (jakarta.jms.MessageProducer)19 Destination (jakarta.jms.Destination)18 MessagingMessageListenerAdapter (org.springframework.jms.listener.adapter.MessagingMessageListenerAdapter)16 Queue (jakarta.jms.Queue)12 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)12 BDDMockito.given (org.mockito.BDDMockito.given)12 Mockito.mock (org.mockito.Mockito.mock)12 Mockito.verify (org.mockito.Mockito.verify)12 StubQueue (org.springframework.jms.StubQueue)12 ObjectMessage (jakarta.jms.ObjectMessage)11 QueueSession (jakarta.jms.QueueSession)11 SimpleMessageConverter (org.springframework.jms.support.converter.SimpleMessageConverter)11