Deep Copy,JUnit测试Java

时间:2013-01-07 19:37:56

标签: java junit deep-copy

我正在尝试执行一个对象的深层副本,然后在不改变oringial的情况下修改新对象。为了测试功能,我正在使用JUnit来确保功能的正常运行。

目前的测试基本上是复制游戏“质量效应3”并改变现在的配音演员。

游戏类

@Override
protected Object clone(){
    Game obj = new Game(getTitle(), getLeadVoiceActor(), getRating());
    return obj;
}

JUnit测试

@Before
public void setUp() {
    p1 = new Person("Mark", "Meer");
    g1 = new Game("Mass Effect 3", p1, 5);
}

@Test
public void testClone() throws CloneNotSupportedException {
    //This works
    Game g2 = (Game)g1.clone();
    assertEquals(g2, g1);
    assertNotSame(g2, g1);

    //This doesn't even though the lead voice actor is now different
    p1 = g1.getLeadVoiceActor();
    p1.setFirstName("Jennifer");
    p1.setLastName("Hale");
    assertFalse(g2.equals(g1));
    assertEquals("Jennifer Hale", g1.getLeadVoiceActor().toString());
    assertEquals("Mark Meer", g2.getLeadVoiceActor().toString());
}

使用junit.framework.assertionfailederror测试失败。

1 个答案:

答案 0 :(得分:3)

如果您的问题是:为什么原始游戏中的人和克隆游戏中的人一样?,那么答案是您不是深度克隆而是浅层克隆。< / p>

调用getLeadVoiceActor()并将结果分配给克隆游戏会将引用传递给克隆游戏的人。它没有任何副本。要制作副本,您需要

protected Object clone(){
    Game obj = new Game(getTitle(), (Person) getLeadVoiceActor().clone(), getRating());
    return obj;
}

protected Object clone(){
    Game obj = new Game(getTitle(), 
                        new Person(getLeadVoiceActor().getFirstName(), getLeadVoiceActor().getLastName()), 
                        getRating());
    return obj;
}

请注意,不建议使用clone()来支持复制构造函数。如果您实施克隆,则应使用super.clone()

另请注意,如果Person不可变,则不必克隆Person。