Java JUnit4:使简单的 assertEquals 测试通过
Java JUnit4: Make simple assertEquals Test pass
我正在尝试使用 assertEquals pass 进行简单测试——在我的例子中:assertEquals(bob,cell.getLifeForm());.
第一次测试assertTrue(success);有效,意味着布尔成功 = cell.addLifeForm(bob);有效。
但是我无法得到 assertEquals(bob,cell.getLifeForm());通过。我相信我必须添加实例变量 LifeForm myLifeForm;所以 Cell class 可以跟踪 LifeForm,现在我需要 return getLifeForm() 中的实例变量以及更新 addLifeForm 以正确修改实例变量(这个有问题) .谢谢你。
TestCell.java:
import static org.junit.Assert.*;
import org.junit.Test;
/**
* The test cases for the Cell class
*
*/
public class TestCell
{
/**
* Checks to see if we change the LifeForm held by the Cell that
* getLifeForm properly responds to this change.
*/
@Test
public void testSetLifeForm()
{
LifeForm bob = new LifeForm("Bob", 40);
LifeForm fred = new LifeForm("Fred", 40);
Cell cell = new Cell();
// The cell is empty so this should work.
boolean success = cell.addLifeForm(bob);
assertTrue(success);
assertEquals(bob,cell.getLifeForm());
// The cell is not empty so this should fail.
success = cell.addLifeForm(fred);
assertFalse(success);
assertEquals(bob,cell.getLifeForm());
}
}
Cell.java:
/**
* A Cell that can hold a LifeForm.
*
*/
public class Cell
{
LifeForm myLifeForm;
//unsure about the instance variable
/**
* Tries to add the LifeForm to the Cell. Will not add if a
* LifeForm is already present.
* @return true if the LifeForm was added the Cell, false otherwise.
*/
public boolean addLifeForm(LifeForm entity)
{
return true;
//modify instance variable
}
/**
* @return the LifeForm in this Cell.
*/
public LifeForm getLifeForm()
{
return myLifeForm;
//return instance variable
}
}
你有两条 assertEquals(bob,cell.getLifeForm());
行。
在第一个中,如果您在 addLifeForm
方法中执行 this.myLifeForm = entity
那么它将通过。在第二个中,如果您在 addLifeForm
方法中执行 if (this.myLifeForm == null) this.myLifeForm = entity
那么它将通过。
在你的情况下,我会说测试工作正常,也就是说,它捕获了一个实现错误。
我正在尝试使用 assertEquals pass 进行简单测试——在我的例子中:assertEquals(bob,cell.getLifeForm());.
第一次测试assertTrue(success);有效,意味着布尔成功 = cell.addLifeForm(bob);有效。
但是我无法得到 assertEquals(bob,cell.getLifeForm());通过。我相信我必须添加实例变量 LifeForm myLifeForm;所以 Cell class 可以跟踪 LifeForm,现在我需要 return getLifeForm() 中的实例变量以及更新 addLifeForm 以正确修改实例变量(这个有问题) .谢谢你。
TestCell.java:
import static org.junit.Assert.*;
import org.junit.Test;
/**
* The test cases for the Cell class
*
*/
public class TestCell
{
/**
* Checks to see if we change the LifeForm held by the Cell that
* getLifeForm properly responds to this change.
*/
@Test
public void testSetLifeForm()
{
LifeForm bob = new LifeForm("Bob", 40);
LifeForm fred = new LifeForm("Fred", 40);
Cell cell = new Cell();
// The cell is empty so this should work.
boolean success = cell.addLifeForm(bob);
assertTrue(success);
assertEquals(bob,cell.getLifeForm());
// The cell is not empty so this should fail.
success = cell.addLifeForm(fred);
assertFalse(success);
assertEquals(bob,cell.getLifeForm());
}
}
Cell.java:
/**
* A Cell that can hold a LifeForm.
*
*/
public class Cell
{
LifeForm myLifeForm;
//unsure about the instance variable
/**
* Tries to add the LifeForm to the Cell. Will not add if a
* LifeForm is already present.
* @return true if the LifeForm was added the Cell, false otherwise.
*/
public boolean addLifeForm(LifeForm entity)
{
return true;
//modify instance variable
}
/**
* @return the LifeForm in this Cell.
*/
public LifeForm getLifeForm()
{
return myLifeForm;
//return instance variable
}
}
你有两条 assertEquals(bob,cell.getLifeForm());
行。
在第一个中,如果您在 addLifeForm
方法中执行 this.myLifeForm = entity
那么它将通过。在第二个中,如果您在 addLifeForm
方法中执行 if (this.myLifeForm == null) this.myLifeForm = entity
那么它将通过。
在你的情况下,我会说测试工作正常,也就是说,它捕获了一个实现错误。