测试用例无法正常工作。问题设置两个数字彼此相等

testcase not working properly. Issue setting two numbers equal to each other

我正在处理一个测试用例,但遇到了问题。我很难让我的测试用例正常工作。这是代码:

    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 ad hour 仍然是 1。有人知道为什么吗?

您发布的代码中存在多个错误 -

1.

public void setHour(int newHour)
{
    newHour = hour;
}

class 约会中没有名为 newHour 的实例变量。 即使它是从 class 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。 所以,

The issue is when I run my testcase it says that newhour is 7 ad hour is still 1

不成立。