DefaultErrorBundle implements ErrorBundle { @Override public String getErrorMessage() { return exception != null ? exception.getMessage() : DEFAULT_ERROR_MESSAGE; } DefaultErrorBundle(Exception exception); @Override Exception getException(); @Override String getErrorMessage();  }
@Test public void shouldReturnTheRightErrorMessageForAGivenException() throws Exception { NullPointerException nullPointerException = new NullPointerException("This cannot be null!"); DefaultErrorBundle defaultErrorBundle = new DefaultErrorBundle(nullPointerException); String errorMessage = defaultErrorBundle.getErrorMessage(); assertThat(errorMessage).isNotNull(); assertThat(errorMessage).isInstanceOf(String.class); assertThat(errorMessage).isEqualTo("This cannot be null!"); }
@Test public void shouldReturnNullErrorMessageForAnExceptionWithoutMessage() throws Exception { NullPointerException nullPointerException = new NullPointerException(); DefaultErrorBundle defaultErrorBundle = new DefaultErrorBundle(nullPointerException); String errorMessage = defaultErrorBundle.getErrorMessage(); assertThat(errorMessage).isNullOrEmpty(); }
@Test public void shouldReturnDefaultErrorMessageGivenANullException() throws Exception { DefaultErrorBundle defaultErrorBundle = new DefaultErrorBundle(null); String errorMessage = defaultErrorBundle.getErrorMessage(); assertThat(errorMessage).isNotNull(); assertThat(errorMessage).isInstanceOf(String.class); assertThat(errorMessage).isEqualTo("Unknown error"); }