Reputation: 137
I have this Java program, but my test gives me this message:
testEmployeeTostring: failed testEmployeeTostring expected <[id[= 1013, name= Jubal Early, job = ]procurement]> but was: <[id[= 1013, name= Jubal Early, job = ] procurement]>
I had to use @Override
and I think that's the problem. I hope someone can figure out the problem with this:
public class Employee {
int id;
String name;
JobType job;
public Employee(int id, String name, JobType job)
{
this.id = id;
this.name = name;
this.job = job;
}
@Override public String toString()
{
return ("["+ "id =" + id + ", name = " + name + ", job = " + job + "]");
}
}
Upvotes: 2
Views: 1126
Reputation: 30723
I believe you have some extra space(s) in the string returned toString() method thus making the returned string to be (slightly) different than the string the test expects.
Upvotes: 1
Reputation: 25705
this probably happened due to JobType.toString() method returning an extra space.
Upvotes: 1
Reputation: 5554
With String a
and String b
,
are you doing an a == b
instead of a.equals(b)
?
Upvotes: -1
Reputation: 340733
Are you sure you have equals()
method overriden? JUnit uses equals()
to compare objects. Overriding hashCode()
is always a good idea as well:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee)) return false;
Employee employee = (Employee) o;
return id == employee.id && job == employee.job && name.equals(employee.name);
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + name.hashCode();
result = 31 * result + job.hashCode();
return result;
}
The code above assumes all fields are non-nullable and that JobType
is an enum
. And BTW toString()
might have nothing to do here, as long as you are comparing objects, not toString()
of objects (bad practice).
Upvotes: 1
Reputation: 1054
There is an extra space in your expected String with the assertion statement.
Upvotes: 2