### Question:
InMemoryTypedMessageListener extends InMemoryMessageListener<TypedMessage> implements TypedMessageListener { @Override public void registerMessageHandler(final String messageType, final MessageHandler<TypedMessage> messageHandler) { messageHandlers.put(messageType, messageHandler); } InMemoryTypedMessageListener(final MessageQueue<TypedMessage> typedMessageQueue,
            final InMemoryMessageQueuePoller inMemoryMessageQueuePoller); @Override void registerMessageHandler(final String messageType, final MessageHandler<TypedMessage> messageHandler);  }### Answer:
@Test public void shouldHandleAndDeleteMessageForRegisteredType_onReceiveAndHandleMessages() throws Exception { final String messageType = randomString(); final TypedMessage mockTypedMessage = mock(TypedMessage.class); when(mockTypedMessage.getType()).thenReturn(messageType); final MessageHandler<TypedMessage> mockMessageHandler = mock(MessageHandler.class); inMemoryMessageListener.registerMessageHandler(messageType, mockMessageHandler); when(mockMessageQueue.receive()).thenReturn(Collections.singletonList(mockTypedMessage)); final boolean polledMessage = inMemoryMessageListener.receiveAndHandleMessages(); assertTrue(polledMessage); verify(mockMessageHandler).handle(mockTypedMessage); verify(mockMessageQueue).delete(mockTypedMessage); }
@Test public void shouldDeleteMessageWhenHandlerThrowsException_onReceiveAndHandleMessages() throws Exception { final String messageType = randomString(); final TypedMessage mockTypedMessage = mock(TypedMessage.class); when(mockTypedMessage.getType()).thenReturn(messageType); final MessageHandler<TypedMessage> mockMessageHandler = mock(MessageHandler.class); inMemoryMessageListener.registerMessageHandler(messageType, mockMessageHandler); when(mockMessageQueue.receive()).thenReturn(Collections.singletonList(mockTypedMessage)); doThrow(MessageHandlingException.class).when(mockMessageHandler).handle(mockTypedMessage); final boolean polledMessage = inMemoryMessageListener.receiveAndHandleMessages(); assertTrue(polledMessage); verify(mockMessageHandler).handle(mockTypedMessage); verify(mockMessageQueue).delete(mockTypedMessage); }