}
+ private X initialValue;
private Detachable<X> detachable;
private DetachableEntity<X> entity;
@Before
public void setUp() {
+ initialValue = mock(X.class);
detachable = mock(Detachable.class);
+ when(detachable.get()).thenReturn(initialValue);
entity = new DetachableEntity<X>(detachable);
}
@Test
public void testGetObject() {
X x = new X();
+ reset(detachable);
when(detachable.get()).thenReturn(x);
X value = entity.getObject();
- assertSame(x, value);
- verify(detachable).get();
+ // we did not detach it yet so the old value should be returned.
+ assertSame(initialValue, value);
+ verifyNoMoreInteractions(detachable);
}
@Test
X x = new X();
when(detachable.get()).thenReturn(x);
- entity.onDetach();
+ entity.detach();
verify(detachable).detach();
+ reset(detachable);
+ when(detachable.get()).thenReturn(x);
+
X y = entity.getObject();
assertSame(x, y);
verify(detachable).get();