JmsSender implements EnvelopeSender { @Override public void send(final JsonEnvelope jsonEnvelope, final String destinationName) { final Destination destination = destinationProvider.getDestination(destinationName); try (final Connection connection = connectionFactory.createConnection(); final Session session = connection.createSession(false, AUTO_ACKNOWLEDGE); final MessageProducer producer = session.createProducer(destination)) { producer.send(envelopeConverter.toMessage(jsonEnvelope, session)); } catch (final JMSException e) { throw new JmsEnvelopeSenderException(format("Exception while sending envelope with name %s", jsonEnvelope.metadata().name()), e); } }  @Override void send(final JsonEnvelope jsonEnvelope, final String destinationName);  }
@Test public void shouldSendJsonEnvelopeToJmsQueue() throws Exception { final String destinationName = "destination name"; final Connection connection = mock(Connection.class); final Session session = mock(Session.class); final MessageProducer messageProducer = mock(MessageProducer.class); final Destination destination = mock(Destination.class); final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class); final TextMessage textMessage = mock(TextMessage.class); when(destinationProvider.getDestination(destinationName)).thenReturn(destination); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createSession(false, AUTO_ACKNOWLEDGE)).thenReturn(session); when(session.createProducer(destination)).thenReturn(messageProducer); when(envelopeConverter.toMessage(jsonEnvelope, session)).thenReturn(textMessage); jmsSender.send(jsonEnvelope, destinationName); verify(messageProducer).send(textMessage); }
@Test public void shouldThrowExceptionIfJmsExceptionIsThrown() throws Exception { final String destinationName = "destination name"; final JsonEnvelope jsonEnvelope = mock(JsonEnvelope.class); final Metadata metadata = mock(Metadata.class); final Destination destination = mock(Destination.class); when(destinationProvider.getDestination(destinationName)).thenReturn(destination); when(connectionFactory.createConnection()).thenThrow(new JMSException("Test exception")); when(jsonEnvelope.metadata()).thenReturn(metadata); when(metadata.name()).thenReturn("command.test"); try { jmsSender.send(jsonEnvelope, destinationName); fail(); } catch (final JmsEnvelopeSenderException e) { assertThat(e.getMessage(), is("Exception while sending envelope with name command.test")); } }