### Question:
ParseRelation implements Parcelable { void ensureParentAndKey(ParseObject someParent, String someKey) { synchronized (mutex) { if (parent == null) { parent = new WeakReference<>(someParent); parentObjectId = someParent.getObjectId(); parentClassName = someParent.getClassName(); } if (key == null) { key = someKey; } if (parent.get() != someParent) { throw new IllegalStateException( "Internal error. One ParseRelation retrieved from two different ParseObjects."); } if (!key.equals(someKey)) { throw new IllegalStateException( "Internal error. One ParseRelation retrieved from two different keys."); } } } ParseRelation(ParseObject parent, String key);   ParseRelation(String targetClass);   ParseRelation(JSONObject jsonObject, ParseDecoder decoder);   ParseRelation(Parcel source, ParseParcelDecoder decoder); void add(T object); void remove(T object); ParseQuery<T> getQuery(); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); final static Creator<ParseRelation> CREATOR; }### Answer:
@Test public void testEnsureParentAndKey() throws Exception { ParseRelation relation = new ParseRelation("Test"); ParseObject parent = mock(ParseObject.class); relation.ensureParentAndKey(parent, "key"); assertEquals(parent, relation.getParent()); assertEquals("key", relation.getKey()); }
@Test public void testEnsureParentAndKeyWithDifferentParent() throws Exception { ParseRelation relation = new ParseRelation(mock(ParseObject.class), "key"); thrown.expect(IllegalStateException.class); thrown.expectMessage( "Internal error. One ParseRelation retrieved from two different ParseObjects."); relation.ensureParentAndKey(new ParseObject("Parent"), "key"); }
@Test public void testEnsureParentAndKeyWithDifferentKey() throws Exception { ParseObject parent = mock(ParseObject.class); ParseRelation relation = new ParseRelation(parent, "key"); thrown.expect(IllegalStateException.class); thrown.expectMessage( "Internal error. One ParseRelation retrieved from two different keys."); relation.ensureParentAndKey(parent, "keyAgain"); }
@Test public void testEnsureParentAndKey() { ParseRelation relation = new ParseRelation("Test"); ParseObject parent = mock(ParseObject.class); relation.ensureParentAndKey(parent, "key"); assertEquals(parent, relation.getParent()); assertEquals("key", relation.getKey()); }
@Test public void testEnsureParentAndKeyWithDifferentParent() { ParseRelation relation = new ParseRelation(mock(ParseObject.class), "key"); thrown.expect(IllegalStateException.class); thrown.expectMessage( "Internal error. One ParseRelation retrieved from two different ParseObjects."); relation.ensureParentAndKey(new ParseObject("Parent"), "key"); }
@Test public void testEnsureParentAndKeyWithDifferentKey() { ParseObject parent = mock(ParseObject.class); ParseRelation relation = new ParseRelation(parent, "key"); thrown.expect(IllegalStateException.class); thrown.expectMessage( "Internal error. One ParseRelation retrieved from two different keys."); relation.ensureParentAndKey(parent, "keyAgain"); }