### Question:
Utils { static <T> Optional<T> cast(Object object, Class<T> type) { return type.isInstance(object) ? Optional.of(type.cast(object)) : Optional.empty(); }    }### Answer:
@Test public void testCast_notAnInstance() { assertThat(Utils.cast(1, String.class)).isEqualTo(Optional.empty()); }
@Test public void testCast_isAnInstance() { assertThat(Utils.cast("hi", String.class)).isEqualTo(Optional.of("hi")); }
@Test public void testCast_null() { assertThat(Utils.cast(null, String.class)).isEqualTo(Optional.empty()); assertThrows(NullPointerException.class, () -> Utils.cast("hi", null)); }