### Question:
SmallSet extends AbstractSet<T> { Set<T> union(final SmallSet<T> otherSet) { if ((otherSet.element1 == element1 && otherSet.element2 == element2) || (otherSet.element1 == element2 && otherSet.element2 == element1)) { return this; } if (otherSet.element1 == null) { return this; } if (element1 == null) { return otherSet; } if (otherSet.element2 == null) { if (element2 == null) { return new SmallSet<>(element1, otherSet.element1); } if (otherSet.element1 == element1 || otherSet.element1 == element2) { return this; } } if (element2 == null && (element1 == otherSet.element1 || element1 == otherSet.element2)) { return otherSet; } HashSet<T> result = new HashSet<>(4); result.add(element1); if (element2 != null) { result.add(element2); } result.add(otherSet.element1); if (otherSet.element2 != null) { result.add(otherSet.element2); } return result; } SmallSet();   SmallSet(final T element); private  SmallSet(final T element1, final T element2); @Override Iterator<T> iterator(); @Override int size();  }### Answer:
@Test public void testUnion_oneElement_emptySet() { SmallSet<Object> set1 = new SmallSet<>(ELEMENT1); SmallSet<Object> set2 = new SmallSet<>(); Set<Object> union1 = set1.union(set2); Set<Object> union2 = set2.union(set1); assertEquals(set1, union1); assertEquals(set1, union2); }
@Test public void testUnion_oneElement_oneElement() { SmallSet<Object> set1 = new SmallSet<>(ELEMENT1); SmallSet<Object> set2 = new SmallSet<>(ELEMENT1); Set<Object> union1 = set1.union(set2); Set<Object> union2 = set2.union(set1); assertEquals(union1, union2); assertEquals(union1, new HashSet<Object>(Arrays.asList(ELEMENT1))); }
@Test public void testUnion_oneElement_twoElements_superSet() { SmallSet<Object> set1 = newSmallSet(ELEMENT1, ELEMENT2); SmallSet<Object> set2 = new SmallSet<>(ELEMENT1); Set<Object> union1 = set1.union(set2); Set<Object> union2 = set2.union(set1); assertEquals(union1, union2); assertEquals(union1, new HashSet<Object>(Arrays.asList(ELEMENT1, ELEMENT2))); }
@Test public void testUnion_twoElements_twoElements_equalSets() { SmallSet<Object> set1 = newSmallSet(ELEMENT1, ELEMENT2); SmallSet<Object> set2 = newSmallSet(ELEMENT2, ELEMENT1); Set<Object> union1 = set1.union(set2); Set<Object> union2 = set2.union(set1); assertEquals(union1, union2); assertEquals(union1, new HashSet<Object>(Arrays.asList(ELEMENT1, ELEMENT2))); }
@Test public void testUnion_twoElements_oneElement_distinctSets() { SmallSet<Object> set1 = newSmallSet(ELEMENT1, ELEMENT2); SmallSet<Object> set2 = new SmallSet<>(ELEMENT3); Set<Object> union1 = set1.union(set2); Set<Object> union2 = set2.union(set1); assertEquals(union1, union2); assertEquals(union1, new HashSet<Object>(Arrays.asList(ELEMENT1, ELEMENT2, ELEMENT3))); }
@Test public void testUnion_twoElements_twoElements_distincSets() { SmallSet<Object> set1 = newSmallSet(ELEMENT1, ELEMENT2); SmallSet<Object> set2 = newSmallSet(ELEMENT3, ELEMENT4); Set<Object> union1 = set1.union(set2); Set<Object> union2 = set2.union(set1); assertEquals(union1, union2); assertEquals( union1, new HashSet<Object>(Arrays.asList(ELEMENT1, ELEMENT2, ELEMENT3, ELEMENT4))); }