我正在研究测试用例,但我遇到了麻烦。我很难让我的测试用例发挥作用。这是代码:
public class Appointment extends Actor
{
int hour;
String description;
public Appointment(int hour, String description)
{
super();
this.hour = hour;
this.description = description;
}
public void setHour(int newHour)
{
newHour = hour;
}
}
/////////
public class AppointmentTest extends TestCase
{
private Appointment appointment;
private int hour;
private String description;
private String newDescription;
private int newHour;
public AppointmentTest()
{
}
public void setUp()
{
appointment = new Appointment(hour, description);
this.hour = hour;
this.description = description;
hour = 7;
description = "";
newHour = 1;
newDescription = "hello";
}
public void testSetHour()
{
appointment.setHour(1);
assertEquals(newHour, hour);
}
}
问题是,当我运行我的测试用例时,它表示newhour是7个小时仍然是1.有人知道为什么吗?
答案 0 :(得分:0)
您发布的代码中出现多个错误 -
1
public void setHour(int newHour)
{
newHour = hour;
}
在Appointment类中没有名为newHour
的实例变量。
即使它从类Actor
继承而来,你也没有使用this.newHour
来设置它,因此你的逻辑在这里似乎有问题。
2
public void setUp()
{
appointment = new Appointment(hour, description);
this.hour = hour;
this.description = description;
hour = 7; //this statement is overriding your this.hour = hour statement. what is the point?
description = ""; //this statement is overriding your this.description = description statement. what is the point?
newHour = 1;
newDescription = "hello";
}
3
public void testSetHour()
{
appointment.setHour(1); // this statement doesn't make any difference for next assertEquals statement. It changes instance variable hour, not the local variable.
assertEquals(newHour, hour);
}
在致电时assertEquals
newHour
为1,hour
为7。
所以,
问题是,当我运行我的测试用例时,它说newhour是7个小时 仍然是1
不成立。