Ronnie Jebara Ronnie Jebara - 19 days ago 10
Java Question

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

I'm working on a test case and I'm having trouble. Im having a hard time with getting my test cases to work. Here is the code:

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);
}


The issue is when I run my testcase it says that newhour is 7 ad hour is still 1. Does anyone know why?

Answer

Multiple mistakes in your posted code -

1.

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

There's no instance variable called newHour in class Appointment. Even if it is getting inherited from class Actor you're not using this.newHour to set it hence your logic seems questionable here.

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);
}

At the time of calling assertEquals newHour is 1 and hour is 7. So,

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

doesn't hold true.