我正在尝试执行一个对象的深层副本,然后在不更改原始对象的情况下修改新对象。为了测试功能,我使用 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 而失败。