### Question:
JMSMessageConsumer { List<Event> take() throws JMSException { List<Event> result = new ArrayList<Event>(batchSize); Message message; message = messageConsumer.receive(pollTimeout); if(message != null) { result.addAll(messageConverter.convert(message)); int max = batchSize - 1; for (int i = 0; i < max; i++) { message = messageConsumer.receiveNoWait(); if(message == null) { break; } result.addAll(messageConverter.convert(message)); } } if(logger.isDebugEnabled()) { logger.debug(String.format("Took batch of %s from %s", result.size(), destination)); } return result; } JMSMessageConsumer(ConnectionFactory connectionFactory,
      String destinationName, JMSDestinationType destinationType,
      String messageSelector, int batchSize, long pollTimeout,
      JMSMessageConverter messageConverter, Optional<String> userName,
      Optional<String> password);   }### Answer:
@Test public void testTopic() throws Exception { destinationType = JMSDestinationType.TOPIC; when(session.createQueue(destinationName)).thenThrow(new AssertionError()); when(session.createTopic(destinationName)).thenReturn(topic); consumer = create(); List<Event> events = consumer.take(); assertEquals(batchSize, events.size()); assertBodyIsExpected(events); }
@Test public void testUserPass() throws Exception { consumer = create(); List<Event> events = consumer.take(); assertEquals(batchSize, events.size()); assertBodyIsExpected(events); }
@Test public void testNoUserPass() throws Exception { userName = Optional.absent(); when(connectionFactory.createConnection(USERNAME, PASSWORD)). thenThrow(new AssertionError()); when(connectionFactory.createConnection()).thenReturn(connection); consumer = create(); List<Event> events = consumer.take(); assertEquals(batchSize, events.size()); assertBodyIsExpected(events); }
@Test public void testNoEvents() throws Exception { when(messageConsumer.receive(anyLong())).thenReturn(null); consumer = create(); List<Event> events = consumer.take(); assertEquals(0, events.size()); verify(messageConsumer, times(1)).receive(anyLong()); verifyNoMoreInteractions(messageConsumer); }
@Test public void testSingleEvent() throws Exception { when(messageConsumer.receiveNoWait()).thenReturn(null); consumer = create(); List<Event> events = consumer.take(); assertEquals(1, events.size()); assertBodyIsExpected(events); }
@Test public void testPartialBatch() throws Exception { when(messageConsumer.receiveNoWait()).thenReturn(message, (Message)null); consumer = create(); List<Event> events = consumer.take(); assertEquals(2, events.size()); assertBodyIsExpected(events); }