Issue
I am very rusty in java and trying to relearn it again in a youtube tutorial, the code is as follows: This is Person.java
package com.gurmeet.javacourse.lesson2;
import com.gurmeet.javacourse.lesson3.Name;
public class Person {
private Name personName;
private static int personCounter;
public Person()
{
personCounter++;
/*
* empty on purpose - default constructor
*/
}
public Person(Name personName) {
this.personName = personName;
}
public String helloWorld() {
// TODO Auto-generated method stub
return "Hello World";
}
public String hello(String name) {
// TODO Auto-generated method stub
return "Hello " + name;
}
public static int numberOfPersons() {
// TODO Auto-generated method stub
return personCounter;
}
}
And this is PersonTest.java
package com.gurmeet.javacourse.lesson2;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class PersonTest {
@Test
public void shouldReturnHelloWorld() {
Person marcus = new Person();
assertEquals("Hello World", marcus.helloWorld());
}
@Test
public void shouldReturnHelloMarcus() {
Person person = new Person();
assertEquals("Hello Marcus", person.hello("Marcus"));
}
@Test
public void shouldReturnNumberOfPersons() {
Person person1 = new Person();
Person person2 = new Person();
Person person3 = new Person();
Person person4 = new Person();
assertEquals(4, Person.numberOfPersons() - 1);
}
}
these two above are in the same package but I created another package in the same project like below:
package com.gurmeet.javacourse.lesson3;
public class Name {
}
I am using JUnit to test my code, but in the for the last testing in my code I keep getting an error, you see the number of persons is supposed to be 4, but the result keeps showing 5. The youtube guy got the correct answer and I didnt even though I followed his coding correctly. What I came to understand in the tutorial is that the static is a global, at class level and since I have two classes created in the same package, the personCounter is not at 1 but 2 at default, therefore the it keeps showing 5 instead of 4. I solved it by subtracting 1 in the method but I dont think that is the correct way. Is my reasoning correct? or is there another explanation. And above all else, how do I solve this? Go easy on me if I made any mistake.
Solution
I guess the main problem is that when you run the first two tests, the static variable personCounter becomes 2 as you are creating two objects. Then when you run the third test, it starts from the 3 and goes upto 6.
I guess if you add another method to set the person counter to zero and call this method before creating the objects in the third test, you would get 4 as your output.
Add this in you class as a function
public static void setNumberOfPersons(int value) { personCounter = value; }
And then call Person.setNumberOfPersons(0);
first in the third test
Edit:
Thanks to seelenvirtuose(see comment) for pointing out the random order part for tests. As said, I suppose the problem would go away by setting the value to 0 in the third test. This way the order would not matter.
Answered By - twothreezarsix
Answer Checked By - Mildred Charles (JavaFixing Admin)