ConsistencyUtils { public static <T> T checkInstance(Object obj, Class<T> type) { if (type.isInstance(obj)) { T t = type.cast(obj); return t; } else { throw new ClassCastException(obj.getClass() + " incompatible type with " + type.getName()); } } 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 testCheckInstanceIncorrect() throws Exception { assertEquals(new Integer(10), ConsistencyUtils.checkInstance(new Integer(10), Integer.class)); }
@Test public void testCheckNullInstance() throws Exception { Integer integer = new Integer(10); thrown.expect(ClassCastException.class); thrown.expectMessage(integer.getClass() + " incompatible type with " + Float.class.getName()); ConsistencyUtils.checkInstance(integer, Float.class); }