NotificationExceptionHandler { public void handleError(Message<MessagingException> errorMessage) { Assert.notNull(errorMessage); MessagingException messagingExceptionPayload = errorMessage.getPayload(); Throwable ex = messagingExceptionPayload.getCause(); Object origMessagePayload = messagingExceptionPayload.getFailedMessage().getPayload(); StringBuilder detailsStringBuilder = new StringBuilder(); detailsStringBuilder.append("Error occured when handling consent signed/revoked message."); detailsStringBuilder.append("\n\n"); detailsStringBuilder.append("Original message payload:"); detailsStringBuilder.append("\n"); detailsStringBuilder.append(origMessagePayload.toString()); detailsStringBuilder.append("\n\n"); detailsStringBuilder.append("Error message:"); detailsStringBuilder.append("\n"); detailsStringBuilder.append(ex.getMessage()); detailsStringBuilder.append("\n\n"); StringWriter errors = new StringWriter(); ex.printStackTrace(new PrintWriter(errors)); detailsStringBuilder.append("Error stack trace:"); detailsStringBuilder.append("\n"); detailsStringBuilder.append(errors.toString()); Notification notification = new Notification(); notification.setDetails(detailsStringBuilder.toString()); notification.setSubject("Consent signed/revoked message hanlding error"); notificationPublisher.publish(notification); }  void handleError(Message<MessagingException> errorMessage);  }
@Test(expected = IllegalArgumentException.class) public void testHandleError_Throws_Exception_Given_Null_Message() { sut.handleError(null); }
@Test public void testHandleError_Notification_Is_Published() { Message<MessagingException> errorMessageMock = (Message<MessagingException>) mock(Message.class); MessagingException messagingExceptionMock = mock(MessagingException.class); when(errorMessageMock.getPayload()).thenReturn(messagingExceptionMock); Message<?> failedMessageMock = (Message<?>) mock(Message.class); doReturn(failedMessageMock).when(messagingExceptionMock) .getFailedMessage(); String origMessagePayload = "origMessagePayload"; doReturn(origMessagePayload).when(failedMessageMock).getPayload(); Throwable exMock = mock(Throwable.class); when(messagingExceptionMock.getCause()).thenReturn(exMock); String expectedDetails = "bla"; String expectedSubject = "Consent signed/revoked message hanlding error"; sut.handleError(errorMessageMock); verify(notificationPublisher, times(1)).publish( argThat(new IsNotificationAsExpected(expectedSubject, expectedDetails))); }