JmsUtils { public static <T> T getFromMessage(Message message, Class<T> tClass) throws JMSException { T result; if (message instanceof ObjectMessage) { final Serializable object = ((ObjectMessage) message).getObject(); if (tClass.isInstance(object)) { result = tClass.cast(object); } else { LOGGER.error("Invalid message object type: {}", object); result = null; } } else { LOGGER.error("Invalid message type: {}", message); result = null; } return result; } private  JmsUtils(); static void closeQuietly(MessageProducer messageProducer); static void closeQuietly(MessageConsumer messageConsumer); static void closeQuietly(Session session); static void closeQuietly(Connection connection); static T getFromMessage(Message message, Class<T> tClass); static String getJMSCorrelationID(Message message); static String getJMSMessageID(Message message);  }
@Test public void getFromMessage_whenStringWrapped_expectString() throws Exception { ObjectMessage message = Mockito.mock(ObjectMessage.class); final String messageText = "This is message"; when(message.getObject()).thenReturn(messageText); final String text = JmsUtils.getFromMessage(message, String.class); assertThat(text, is(messageText)); }
@Test public void getFromMessage_whenLongWrappedAndStringExpected_expectNull() throws Exception { ObjectMessage message = Mockito.mock(ObjectMessage.class); when(message.getObject()).thenReturn(100L); final String text = JmsUtils.getFromMessage(message, String.class); assertNull(text); }