2013-09-28 77 views
-1

我是JUnit Testing的新成员,对它的工作原理感到好奇。目前我正在尝试通过单元测试。方法的Java 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,因为第一个测试项断言为真。我的逻辑正确吗?

只是为了清楚这里是我的完整的测试类一切都已经存在。

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))); 
    } 

这是我的完整的人类是抽象的。我已经有了第一个测试案例。

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的类扩展了这个Abstract Class。

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); 
    } 

} 

我能够通过运气得到第一个测试工作。我不知道单元测试如何知道如何测试我的值。

+0

不清楚,你的程序在哪里等于方法?对名为Person的抽象类使用 –

+0

。 –

+0

以及在哪里定义了名为'employees'的变量? –

回答

0

如果你在person类中实现了equals,那么你可以断言两个人是否相等。

你有什么以上,相当于

Person p1 = employees.get(0); 
Person p2 = employees.get(2); 
assertTrue(p1.equals(p2)); 

上面,你直接调用equals方法。

但你可能只是以及做

assertEquals(p1,p2); 

在他的案件的Person.equals方法引擎盖下调用。

方法assertEqualsassertTrueassertFalse和其他几个人可以从org.junit.Assert类被静态导入了,有点像:

import static org.junit.Assert.assertTrue; 
import static org.junit.Assert.assertFalse; 
import static org.junit.Assert.assertEquals; 

或者更简单的形式import static org.junit.Assert.*;

您实现平等的应该简单地验证如果一名员工与另一名员工相等,则不应该像你似乎暗示的那样了解员工名单。

例如

class Person { 
    public boolean equals(Object o) { 
     if(this==o) { 
     return true; 
     } 
     if(o instanceof Person) { 
     Person other = (Person) o; 
     return (this.name == other.name) || (this.name != null && this.name.equals(other.name); 
     } 
     reurn false; 
    } 
} 

您的测试将包括在验证这个equals实现是正确的,换句话说,它满足像simmetry,传递性和反思性,等等。参见Object.equals方法的javadoc性质更多地了解这一点。

+0

Asserts都在测试类上。 –

0

edwin说道。另外:

测试应该调用equals并断言返回的布尔值。

这样做可能会引起混淆,因为没有明显的方法来设置员工价值。通常的方法是让employee字段包可见(无可访问性修饰符),并将Test类与被测试的类放在同一个包中。

编辑:如果Person类是抽象的,可以考虑创建一个具体的子类作为一个样本,允许您单元测试equals方法。

0

首先让我说明你的equals方法是而不是在测试中被调用。您正在有效呼叫Object.equals(Object o)而不是Person.equals()。你注意到签名的不同吗?你的equals方法不采取参数,这是不寻常的 - 你应该比较等效东西别的。

此外,您的测试的设置是不恰当的。您应该建立一个单元的测试数据进行验证。放弃不必要的数据结构;我的意思是你只是比较equals()电话中的两个对象。

让我们来解决这两个问题。您的equals方法需要覆盖Object.equals。要做到这一点,你需要制作一个具有相同签名的方法。

@Override 
public boolean equals(Object other) { 
    // What makes two Person instances equivalent? 
} 

我会将此实施留给您。考虑到othernull的情况。

接下来,测试。我更喜欢设置我的测试的方式是小块。测试此逻辑时有几个方面需要验证:

  • 这两个实例的值是否相同?
  • 它们是否与hashCode()具有相同的值? (这是一个约定,如果两个对象相等,则它们的hashCode是相等的。)
  • 如果实例不同,它们是否相等?例如。比较PersonInteger应该是...
  • 如果其中一个实例是null,它们是否相等?

至少,这是四个测试用例。

@Test 
public void equals_equivalentInstances() { 
    // given 
    // two objects that are initialized in the exact same way 
    // when + then 
    // call the equals() method and assert that the statement is true 
} 


@Test 
public void equals_hashCodeMatches() { 
    // given 
    // two objects that are initialized in the exact same way 
    // when + then 
    // call the hashCode() method of both and assert their equivalence 
} 


@Test 
public void equals_handlesDifferentObjectTypes() { 
    // given 
    // a Person class and some other Object 
    // when + then 
    // call the equals() method and assert that they are not equal 
} 

@Test 
public void equals_handlesNull() { 
    // given 
    // two Person classes, one of them initialized to null 
    // when + then 
    // call the equals() method and assert that they are not equal 
}