1

I'm working on a test case and I'm having trouble. I'm 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?

Raman Shrivastava
  • 2,923
  • 15
  • 26
  • There is no code that changes either `hour` or `newHour`... Are you sure you've posted all related code? (See [MCVE] for guidance) – Alexei Levenkov Nov 21 '16 at 17:54
  • I mean I can post all my code. But thats all the code that I think is relevant to the problem. Would it help if I just post everything. – Ronnie Jebara Nov 21 '16 at 17:59
  • If that the case you may want to read something about Java... Looks like you assume that values are passed by reference - check out http://stackoverflow.com/questions/3326112/java-best-way-to-pass-int-by-reference – Alexei Levenkov Nov 21 '16 at 18:08

1 Answers1

0

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.

Raman Shrivastava
  • 2,923
  • 15
  • 26