### Question:
SafeComparator implements Comparator<T> { @Override public int compare(final T obj1, final T obj2) { if (obj1 == null) { return obj2 != null ? -1 : 0; } else if (obj2 == null) { return 1; } else { return comparatorSupplier.get().compare(obj1, obj2); } } private  SafeComparator(Supplier<Comparator<T>> comparatorSupplier); static SafeComparator<K> of(Supplier<Comparator<K>> comparatorSupplier); @Override int compare(final T obj1,
                       final T obj2); static final SafeComparator<Object> TO_STRING_COMPARATOR; static final SafeComparator<Object> TO_STRING_REVERSE_COMPARATOR; }### Answer:
@Test public void testWhenFirstParamIsNull() { assertEquals(-1, safeComparator.compare(null, param2)); verify(comparator, never()).compare(any(), any()); }
@Test public void testWhenSecondParamIsNull() { assertEquals(1, safeComparator.compare(param1, null)); verify(comparator, never()).compare(any(), any()); }
@Test public void testWhenBothParamsAreNull() { assertEquals(0, safeComparator.compare(null, null)); verify(comparator, never()).compare(any(), any()); }
@Test public void testWhenBothParamsAreNotNull() { int expectedResult = 1234; when(comparator.compare(param1, param2)).thenReturn(expectedResult); assertEquals(expectedResult, safeComparator.compare(param1, param2)); verify(comparator).compare(param1, param2); }
@Test public void testToStringComparator() { when(param1.toString()).thenReturn("value1"); when(param2.toString()).thenReturn("value2"); assertEquals(-1, TO_STRING_COMPARATOR.compare(param1, param2)); assertEquals(0, TO_STRING_COMPARATOR.compare(param1, param1)); assertEquals(1, TO_STRING_COMPARATOR.compare(param2, param1)); }
@Test public void testToStringReverseComparator() { when(param1.toString()).thenReturn("value1"); when(param2.toString()).thenReturn("value2"); assertEquals(-1, TO_STRING_REVERSE_COMPARATOR.compare(param2, param1)); assertEquals(0, TO_STRING_REVERSE_COMPARATOR.compare(param1, param1)); assertEquals(1, TO_STRING_REVERSE_COMPARATOR.compare(param1, param2)); }