### Question:
ErrorMessageUtil { public static String buildErrorMessage(final Throwable throwable) { if (throwable == null) { return ""; } final List<String> messages = getErrorMessages(throwable); dedup(messages); final String msg = messages.remove(0); final String causeMsg = messages.stream() .map(cause -> PREFIX + cause) .collect(Collectors.joining(System.lineSeparator())); return causeMsg.isEmpty() ? msg : msg + System.lineSeparator() + causeMsg; }  static String buildErrorMessage(final Throwable throwable);  }### Answer:
@Test public void shouldBuildSimpleErrorMessage() { assertThat( buildErrorMessage(new SQLDataException("some message", "some state", 422)), is("some message") ); }
@Test public void shouldUseCustomMsgForConnectException() { assertThat( buildErrorMessage(new ConnectException("asdf")), is("Could not connect to the server.") ); }
@Test public void shouldBuildErrorMessageFromExceptionWithNoMessage() { assertThat( buildErrorMessage(new NullPointerException()), is("java.lang.NullPointerException") ); }
@Test public void shouldBuildErrorMessageFromExceptionChain() { final Throwable cause = new TestException("Something went wrong"); final Throwable subLevel2 = new TestException("Intermediate message 2", cause); final Throwable subLevel1 = new TestException("Intermediate message 1", subLevel2); final Throwable e = new TestException("Top level", subLevel1); assertThat( buildErrorMessage(e), is("Top level" + System.lineSeparator() + "Caused by: Intermediate message 1" + System.lineSeparator() + "Caused by: Intermediate message 2" + System.lineSeparator() + "Caused by: Something went wrong") ); }
@Test public void shouldDeduplicateMessage() { final Throwable cause = new TestException("Something went wrong"); final Throwable subLevel2 = new TestException("Msg that matches", cause); final Throwable subLevel1 = new TestException("Msg that matches", subLevel2); final Throwable e = new TestException("Msg that matches", subLevel1); assertThat( buildErrorMessage(e), is("Msg that matches" + System.lineSeparator() + "Caused by: Something went wrong") ); }
@Test public void shouldNotDeduplicateMessageIfNextMessageIsLonger() { final Throwable cause = new TestException("Something went wrong"); final Throwable subLevel1 = new TestException("Some Message with more detail", cause); final Throwable e = new TestException("Some Message", subLevel1); assertThat( buildErrorMessage(e), is("Some Message" + System.lineSeparator() + "Caused by: Some Message with more detail" + System.lineSeparator() + "Caused by: Something went wrong") ); }
@Test public void shouldHandleRecursiveExceptions() { assertThat( buildErrorMessage(new RecursiveException("It went boom")), is("It went boom") ); }
@Test public void shouldHandleRecursiveExceptionChain() { final Exception cause = new TestException("Something went wrong"); final Throwable e = new TestException("Top level", cause); cause.initCause(e); assertThat( buildErrorMessage(e), is("Top level" + System.lineSeparator() + "Caused by: Something went wrong") ); }