ValidationUtils { public static String validationResultToString(ValidationResultBuilder validationResultBuilder) { StringBuilder stringBuilder = new StringBuilder(); if (validationResultBuilder != null && validationResultBuilder.hasErrors()) { stringBuilder.append("Validation failed. "); stringBuilder.append(validationResultBuilder.getErrorMessages().size()); stringBuilder.append(" errors were found:" + StringUtils.LF); int counter = 1; for (ErrorMessage errorMessage : validationResultBuilder.getErrorMessages()) { stringBuilder.append("Error " + counter + StringUtils.LF); stringBuilder.append("\tMessage: "); stringBuilder.append(errorMessage.getMessage()); stringBuilder.append(StringUtils.LF); if (errorMessage.getThrowable() != null) { stringBuilder.append("\tCaused by: "); stringBuilder.append(errorMessage.getThrowable()); stringBuilder.append(StringUtils.LF); } counter++; } } return stringBuilder.toString(); } private  ValidationUtils(); static String validationResultToString(ValidationResultBuilder validationResultBuilder);  }
@Test public void validationResultToString_whenResultBuilderIsNull_expectEmptyString() throws Exception { String result = ValidationUtils.validationResultToString(null); assertTrue(StringUtils.isBlank(result)); }
@Test public void validationResultToString_whenResultBuilderHasNoErrors_expectEmptyString() throws Exception { String result = ValidationUtils.validationResultToString(builderWithoutErrors); assertTrue(StringUtils.isBlank(result)); }
@Test public void validationResultToString_whenResultBuilderHasErrorsWithoutThrowable_expectMessageWithoutCause() throws Exception { String result = ValidationUtils.validationResultToString(builderWithErrors); assertThat(result, is("Validation failed. 1 errors were found:\nError 1\n\tMessage: message\n")); }
@Test public void validationResultToString_whenResultBuilderHasErrorsAndThrowable_expectMessageWithCause() throws Exception { Throwable throwable = Mockito.mock(Throwable.class); when(throwable.toString()).thenReturn("EXCEPTION"); when(errorMessage.getThrowable()).thenReturn(throwable); String result = ValidationUtils.validationResultToString(builderWithErrors); assertThat(result, is("Validation failed. 1 errors were found:\nError 1\n\tMessage: message\n\tCaused by: EXCEPTION\n")); }