@Test(expected = MessageDeliveryException.class) public void validateExceptionIfNoErrorChannel() { DirectChannel outChannel = new DirectChannel(); outChannel.subscribe(message -> { throw new RuntimeException("problems"); }); MessageProducerSupport mps = new MessageProducerSupport() { }; mps.setOutputChannel(outChannel); mps.setBeanFactory(this.context); mps.afterPropertiesSet(); mps.start(); mps.sendMessage(new GenericMessage<>("hello")); }
@Test(expected = MessageDeliveryException.class) public void validateExceptionIfSendToErrorChannelFails() { DirectChannel outChannel = new DirectChannel(); outChannel.subscribe(message -> { throw new RuntimeException("problems"); }); PublishSubscribeChannel errorChannel = new PublishSubscribeChannel(); errorChannel.subscribe(message -> { throw new RuntimeException("ooops"); }); MessageProducerSupport mps = new MessageProducerSupport() { }; mps.setOutputChannel(outChannel); mps.setErrorChannel(errorChannel); mps.setBeanFactory(this.context); mps.afterPropertiesSet(); mps.start(); mps.sendMessage(new GenericMessage<>("hello")); }
@Test public void testWithChannelName() { DirectChannel outChannel = new DirectChannel(); MessageProducerSupport mps = new MessageProducerSupport() { }; mps.setOutputChannelName("foo"); this.context.registerBean("foo", outChannel); this.context.refresh(); mps.setBeanFactory(this.context); mps.afterPropertiesSet(); mps.start(); assertSame(outChannel, mps.getOutputChannel()); }
@Test public void validateSuccessfulErrorFlowDoesNotThrowErrors() { this.context.refresh(); DirectChannel outChannel = new DirectChannel(); outChannel.subscribe(message -> { throw new RuntimeException("problems"); }); PublishSubscribeChannel errorChannel = new PublishSubscribeChannel(); SuccessfulErrorService errorService = new SuccessfulErrorService(); ServiceActivatingHandler handler = new ServiceActivatingHandler(errorService); handler.setBeanFactory(this.context); handler.afterPropertiesSet(); errorChannel.subscribe(handler); MessageProducerSupport mps = new MessageProducerSupport() { }; mps.setOutputChannel(outChannel); mps.setErrorChannel(errorChannel); mps.setBeanFactory(this.context); mps.afterPropertiesSet(); mps.start(); Message<?> message = new GenericMessage<>("hello"); mps.sendMessage(message); assertThat(errorService.lastMessage, instanceOf(ErrorMessage.class)); ErrorMessage errorMessage = (ErrorMessage) errorService.lastMessage; assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass()); MessageDeliveryException exception = (MessageDeliveryException) errorMessage.getPayload(); assertEquals(message, exception.getFailedMessage()); }