DefaultEnvelopeConverter implements EnvelopeConverter { @Override public TextMessage toMessage(final JsonEnvelope envelope, final Session session) { final String envelopeAsString = jsonObjectEnvelopeConverter.asJsonString(envelope); try { final TextMessage textMessage = session.createTextMessage(envelopeAsString); textMessage.setStringProperty(JMS_HEADER_CPPNAME, envelope.metadata().name()); return textMessage; } catch (JMSException e) { throw new JmsConverterException(String.format("Exception while creating message from envelope %s", envelopeAsString), e); } }  @Override JsonEnvelope fromMessage(final TextMessage message); @Override TextMessage toMessage(final JsonEnvelope envelope, final Session session);  }
@Test public void shouldReturnMessage() throws Exception { when(jsonObjectEnvelopeConverter.asJsonString(envelope)).thenReturn(MESSAGE_TEXT); when(session.createTextMessage(MESSAGE_TEXT)).thenReturn(textMessage); when(envelope.metadata()).thenReturn(metadata); when(metadata.name()).thenReturn(NAME); TextMessage actualTextMessage = envelopeConverter.toMessage(envelope, session); assertThat(actualTextMessage, equalTo(textMessage)); verify(textMessage).setStringProperty(JMS_HEADER_CPPNAME, NAME); }
@Test(expected = JmsConverterException.class) public void shouldThrowExceptionWhenFailToCreateTextMessage() throws JMSException { when(jsonObjectEnvelopeConverter.asJsonString(envelope)).thenReturn(MESSAGE_TEXT); doThrow(JMSException.class).when(session).createTextMessage(MESSAGE_TEXT); envelopeConverter.toMessage(envelope, session); }