我是 JUnit 测试的新手,对它的工作原理感到好奇。目前我正在尝试通过单元测试。
测试方法是
@Test
public void ensure_equals_method_is_properly_coded ()
{
assertTrue(employees.get(0).equals(employees.get(2)));
assertFalse(employees.get(0).equals(employees.get(1)));
assertFalse(employees.get(0).equals(employees.get(3)));
}
我有一个 ArrayList 已经填充了其中的值。据我了解,我想编写一个名为 equals() 的方法来让这个测试通过我的任务。我的问题是这种方法将如何找到要测试的方法。我在名为 Persons 的类中创建了一个 equals() 方法,但我不知道在运行测试时它是否被调用。
我的第二个问题是质疑我的 equal() 方法中的逻辑。到目前为止,我有这个。
public boolean equals() {
if (employees.get(0).equals(employees.get(2)))
return true;
return false;
}
这应该返回 true,因为第一个测试项断言它是 true。我的逻辑对此是否正确?
只是为了清楚起见,这是我的完整测试课程,一切都已经存在。
public class Tests
{
List<Person> employees = new ArrayList<Person>();
@Before
public void init ()
{
Person anEmployee = new Employee();
anEmployee.setName("Trevor Page");
anEmployee.setSex("Male");
Calendar cal = Calendar.getInstance();
cal.set(1983, 0, 1);
anEmployee.setBirthday(cal.getTime());
((Employee)anEmployee).setJobTitle("Sr. Software Engineer");
((Employee)anEmployee).setOrganization(new Google("Google"));
employees.add(anEmployee);
anEmployee = new Employee();
anEmployee.setName("Jane Doe");
anEmployee.setSex("Female");
anEmployee.setBirthday(cal.getTime());
((Employee)anEmployee).setJobTitle("Sr. Software Engineer");
((Employee)anEmployee).setOrganization(new Google("Google"));
employees.add(anEmployee);
anEmployee = new Employee();
anEmployee.setName("Trevor Page");
anEmployee.setSex("Male");
anEmployee.setBirthday(cal.getTime());
((Employee)anEmployee).setJobTitle("Sr. Software Engineer");
((Employee)anEmployee).setOrganization(new Google("Google"));
employees.add(anEmployee);
anEmployee = new Employee();
anEmployee.setName("Trevor Page");
anEmployee.setSex("Male");
anEmployee.setBirthday(cal.getTime());
((Employee)anEmployee).setJobTitle("Sr. Software Engineer");
((Employee)anEmployee).setOrganization(new Microsoft("Microsoft"));
employees.add(anEmployee);
}
@Test
public void ensure_toString_method_is_properly_coded()
{
String message = "Name: Trevor Page, Sex: Male" +
"\nJob Title: Sr. Software Engineer, Organization: Google";
assertThat(employees.get(0).toString(), is(message));
}
@Test
public void ensure_equals_method_is_properly_coded ()
{
assertTrue(employees.get(0).equals(employees.get(2)));
assertFalse(employees.get(0).equals(employees.get(1)));
assertFalse(employees.get(0).equals(employees.get(3)));
}
这是我的完整 Persons 类,它是抽象的。我已经有了第一个测试用例。
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public abstract class Person
{
public abstract String getName();
public abstract String getSex();
public abstract void setName(String name);
public abstract void setSex(String sex);
public abstract void setBirthday(Date birthdate);
public abstract Date getBirthday();
public abstract String getJobTitle();
public abstract String getNameOfOrganization();
List<Person> employees = new ArrayList<Person>();
@Override
public String toString() {
return "Name: " + getName() + ", Sex: " + getSex() + "\n" + "Job Title: " + getJobTitle() + ", Organization: " + getNameOfOrganization() ;
}
}
我有另一个名为 Employee 的类扩展了这个抽象类。
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class Employee extends Person
{
List<String> employees = new ArrayList<String>();
@Override
public String getName() {
employees.add("Trevor Page");
return employees.get(0);
}
@Override
public String getSex() {
employees.add("Male");
return employees.get(1);
}
@Override
public void setName(String name) {
// TODO Auto-generated method stub
}
@Override
public void setSex(String sex) {
// TODO Auto-generated method stub
}
@Override
public void setBirthday(Date birthdate) {
// TODO Auto-generated method stub
}
@Override
public Date getBirthday() {
// TODO Auto-generated method stub
return null;
}
public void setJobTitle(String string) {
// TODO Auto-generated method stub
}
public String getJobTitle() {
employees.add("Sr. Software Engineer");
return employees.get(2);
}
public void setOrganization(Google google) {
// TODO Auto-generated method stub
}
public void setOrganization(Microsoft microsoft) {
// TODO Auto-generated method stub
}
@Override
public String getNameOfOrganization() {
employees.add("Google");
return employees.get(3);
}
}
我能够通过运气进行第一次测试。我不确定单元测试如何知道测试我的价值观。