ConsistencyUtils { public static <T> Collection<T> containsNoNulls(Collection<T> container) { container = checkNotNull(container); for (T item : container) { if (item == null) { throw new NullPointerException("Container cannot contain null values"); } } return container; } private  ConsistencyUtils(); static T checkNotNull(T object); static T checkNotNull(T object, String message); static String checkEmptyString(String object, String message); static String checkEmptyString(String object); static T checkInstance(Object obj, Class<T> type); static Collection<T> checkNotEmpty(Collection<T> container); static Collection<T> containsNoNulls(Collection<T> container);  }
@Test public void shouldReturnSameObjectWhenCollectionNotHasNullItems() throws Exception { List<String> list = new ArrayList<>(); list.add("0"); Collection<String> returnedList = ConsistencyUtils.containsNoNulls(list); assertSame(list, returnedList); }
@Test public void shouldThrowNullPointerExceptionWhenListIsNull() throws Exception { thrown.expect(NullPointerException.class); thrown.expectMessage("The object is null"); List<String> list = null; ConsistencyUtils.containsNoNulls(list); }
@Test public void shouldThrowNullPointerExceptionWhenCollectionHasNullItems() throws Exception { thrown.expect(NullPointerException.class); thrown.expectMessage("Container cannot contain null values"); List<String> list = new ArrayList<>(); list.add(null); ConsistencyUtils.containsNoNulls(list); }