ThreadSafeSwapper { public T swap(T newObject) { synchronized (lock_object) { T oldObject = currentObject; currentObject = newObject; return oldObject; } }  T swap(T newObject); boolean setIfCurrentlyNull(T newObject);  }
@Test public void swapReturnsSwappedOutObject() { Whitebox.setInternalState(testSubject, "currentObject", mockOldObject); GenericTestObject actualReturnedObject = testSubject.swap(mockNewObject); Assert.assertEquals(mockOldObject, actualReturnedObject); }
@Test public void swapReplacesCurrentObjectWithMethodParameter() { Whitebox.setInternalState(testSubject, "currentObject", mockOldObject); GenericTestObject actualOldObject = testSubject.swap(mockIntermediateObject); GenericTestObject actualIntermediateObject = testSubject.swap(mockNewObject); Assert.assertEquals(mockOldObject, actualOldObject); Assert.assertEquals(mockIntermediateObject, actualIntermediateObject); }