I have started practicing TDD approach. I am pretty much new to unit testing.
I would like to know how to test some object returned by an method?
for example if I have following classes (please forgive me for public variables)
class Person {
public String firstName;
public String lastName;
public int age;
private void getFirstAndLastName(fullName) {
// some logic to split name into first name and last name
// and then assign first name and last name to data members
}
public Person(String fullName) {
getFirstAndLastName(fullName);
}
}
and person creator class
public class PersonBuilder {
public static Person buildPerson(String fullName) {
return new Person("Sachin Tendulkar");
}
}
I want to test the output of buildPerson() method of PersonBuilder class.
If I want to make sure that the firsName of object returned by buildPerson() method is ‘Sachin’ and lastName is ‘Tendulkar’ then how should I write test code for this?
Should I check member variables of Person class manually like object.FirstName.equals("Sachin") or is there any other better way to test in this kind of situation? What
is the standard way to test it?
and by the way, I am using Java and JUnit.
Please enlighten !!!
Yes, in your case you would check the member variables.
But you really should use properties instead of public variables. You would check the properties then.