Preconditions { public static <T> T checkNotNullWithMessage(T reference, String message) { if (reference == null) { throw new NullPointerException(message); } return reference; } @VisibleForTesting  Preconditions(); static T checkNotNull(T reference); static T checkNotNull(T reference, String name); static T checkNotNullWithMessage(T reference, String message); static T checkNotNullWithMessage(T reference, String messageFormat, Object... args);  }
@Test public void shouldThrow_withCorrectErrorMessageFromTemplate() { try { Preconditions.checkNotNullWithMessage(null, ERROR_MESSAGE_TEMPLATE, ERROR_MESSAGE_ARG); fail("NullPointerException expected"); } catch (NullPointerException npe) { assertThat(npe).hasMessage(ERROR_MESSAGE_RESULT); } }
@Test public void shouldReturnCheckingReference_whenNotNull_andWithErrorMessageTemplate() { String result = Preconditions .checkNotNullWithMessage(NON_NULL_ARG, ERROR_MESSAGE_TEMPLATE, ERROR_MESSAGE_ARG); assertThat(NON_NULL_ARG).isEqualTo(result); }