### Question:
IterableUtils { public static <T> T getSingleOrNull(Iterator<T> iterator) { T result = null; if (iterator.hasNext()) { result = iterator.next(); } if (iterator.hasNext()) { throw new IllegalStateException("Iterable has more than one element, which is unexpected"); } return result; } private  IterableUtils(); static long countNodes(GraphDatabaseService database); static long count(Iterable iterable); static boolean contains(Iterable<T> iterable, T object); static List<T> toList(Iterable<T> iterable); static T random(Iterable<T> iterable); static Iterable<T> random(Iterable<T> iterable, int numberOfSamples); static T getSingle(Iterator<T> iterator, String notFoundMessage); static T getSingle(Iterable<T> iterable, String notFoundMessage); static T getSingle(Iterator<T> iterator); static T getSingle(Iterable<T> iterable); static T getSingleOrNull(Iterator<T> iterator); static T getSingleOrNull(Iterable<T> iterable); static T getFirst(Iterator<T> iterator, String notFoundMessage); static T getFirst(Iterable<T> iterable, String notFoundMessage); static T getFirstOrNull(Iterator<T> iterator); static T getFirstOrNull(Iterable<T> iterable);  }### Answer:
@Test public void singleElementShouldBeReturnedWhenIterableHasOneElement() { assertEquals("test", getSingleOrNull(Collections.singletonList("test"))); }
@Test public void nullShouldBeReturnedWhenIterableHasNoElements() { assertNull(getSingleOrNull(Collections.emptyList())); }
@Test(expected = IllegalStateException.class) public void exceptionShouldBeThrownWhenIterableHasMoreThanOneElement() { getSingleOrNull(Arrays.asList("test1", "test2")); }