@Test public void exceptionExpected() { try { shouldFail.send(new GenericMessage<String>("foo")); fail("Exception expected"); } catch (MessageHandlingException e) { assertThat(e.getMessage(), startsWith("Unable to find outbound socket")); } }
/** * Test uses the Fail mode of the File Outbound Gateway. When persisting * a payload using the File Outbound Gateway and the mode is set to Fail, * then the destination {@link File} will be created and written if it does * not yet exist. BUT if the destination {@link File} already exists, a * {@link MessageHandlingException} will be thrown. * */ @Test public void gatewayWithFailMode() throws Exception { final MessagingTemplate messagingTemplate = new MessagingTemplate(); messagingTemplate.setDefaultDestination(this.gatewayWithFailModeChannel); String expectedFileContent = "Initial File Content:"; File testFile = new File("test/fileToAppend.txt"); if (testFile.exists()) { testFile.delete(); } messagingTemplate.sendAndReceive(new GenericMessage<>("Initial File Content:")); final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile)); assertEquals(expectedFileContent, actualFileContent); try { messagingTemplate.sendAndReceive(new GenericMessage<>("String content:")); } catch (MessageHandlingException e) { assertThat(e.getMessage(), startsWith("The destination file already exists at '")); return; } fail("Was expecting a MessageHandlingException to be thrown."); }
/** * Test is exactly the same as {@link #gatewayWithFailMode()}. However, the * mode is provided in lower-case ensuring that the mode can be provided * in an case-insensitive fashion. * * Instead a {@link MessageHandlingException} will be thrown. * */ @Test public void gatewayWithFailModeLowercase() throws Exception { final MessagingTemplate messagingTemplate = new MessagingTemplate(); messagingTemplate.setDefaultDestination(this.gatewayWithFailModeLowercaseChannel); String expectedFileContent = "Initial File Content:"; File testFile = new File("test/fileToAppend.txt"); if (testFile.exists()) { testFile.delete(); } messagingTemplate.sendAndReceive(new GenericMessage<>("Initial File Content:")); final String actualFileContent = new String(FileCopyUtils.copyToByteArray(testFile)); assertEquals(expectedFileContent, actualFileContent); try { messagingTemplate.sendAndReceive(new GenericMessage<>("String content:")); } catch (MessageHandlingException e) { assertThat(e.getMessage(), startsWith("The destination file already exists at '")); return; } fail("Was expecting a MessageHandlingException to be thrown."); }
assertThat(e.getMessage(), containsString("already exists")); assertThat(e.getMessage(), containsString("already exists"));
assertThat(e.getMessage(), Matchers.containsString("Unable to find outbound socket"));
assertThat(e.getMessage(), containsString("Failed to clone payload object")); return;