HandlerRoutingReceiver implements IMessageReceiver { @Override public void receive(final IMessageProcessor processor) throws MessageReceiveException, AsynchronousOperationException { try { Object handlerId = handlerField.in(processor.getSequence().getCurrentReceiverArguments()); IMessageReceiver handlerReceiver = handlerReceiversMap.get(handlerId); if (null == handlerReceiver) { throw new MessageReceiveException("Handler not found."); } else { handlerReceiver.receive(processor); } } catch (ReadValueException | InvalidArgumentException e) { throw new MessageReceiveException("Error reading handler name."); } } HandlerRoutingReceiver(final Map<Object, IMessageReceiver> handlerReceiversMap); @Override void receive(final IMessageProcessor processor);  }
@Test(expected = MessageReceiveException.class) public void Should_wrapException_WhenFieldThrows() throws Exception { when(handlerFieldMock.in(same(argsMock))).thenThrow(new ReadValueException()); new HandlerRoutingReceiver(mapMock).receive(messageProcessorMock); }
@Test(expected = MessageReceiveException.class) public void Should_throw_WhenHandlerReceiverIsNotFound() throws Exception { when(handlerFieldMock.in(same(argsMock))).thenReturn("notExist"); when(mapMock.get(eq("notExist"))).thenReturn(null); new HandlerRoutingReceiver(mapMock).receive(messageProcessorMock); }
@Test public void Should_callNestedReceiver_WhenItExist() throws Exception { IMessageReceiver theReceiverMock = mock(IMessageReceiver.class); when(handlerFieldMock.in(same(argsMock))).thenReturn("theReceiver"); when(mapMock.get(eq("theReceiver"))).thenReturn(theReceiverMock); new HandlerRoutingReceiver(mapMock).receive(messageProcessorMock); verify(theReceiverMock).receive(same(messageProcessorMock)); }