ThreadSafeSwapper { public boolean setIfCurrentlyNull(T newObject) { synchronized (lock_object) { T oldObject = currentObject; if (oldObject != null) { return false; } currentObject = newObject; return true; } }  T swap(T newObject); boolean setIfCurrentlyNull(T newObject);  }
@Test public void setIfCurrentlyNullDoesNotSetCurrentObjectIfNotNull() { boolean expectedReturnValue = false; Whitebox.setInternalState(testSubject, "currentObject", mockOldObject); boolean actualReturnValue = testSubject.setIfCurrentlyNull(mockNewObject); Assert.assertEquals(expectedReturnValue, actualReturnValue); }
@Test public void setIfCurrentlyNullSetsCurrentObjectIfNull() { boolean expectedReturnValue = true; Whitebox.setInternalState(testSubject, "currentObject", (GenericTestObject) null); boolean actualReturnValue = testSubject.setIfCurrentlyNull(mockNewObject); Assert.assertEquals(expectedReturnValue, actualReturnValue); }