测试重写的 equals 方法
Testing an overriden equals method
我正在阅读 J. Bloch 的有效 Java,需要复习 equals
/hashCode
合同和关系。
我有以下 JavaBeans class:
public class MyJavaBean{
private int id;
private Properties fItem; //Enumeration type
private String value;
private Rule rule; //Enumeration type
private int fId;
//GET, SET
@Override
public boolean equals(Object o){
if(!(o instanceof MyJavaBean))
return false;
MyJavaBeanv = (MyJavaBean) o;
return (fItem == null ? v.fItem == null : fItem.equals(v.fItem)) &&
(value == null ? v.value == null : value.equals(v.value)) &&
(rule == null ? v.rule == null : rule.equals(v.rule)) &&
fId == v.fId && id == v.id;
}
@Override
public int hashCode(){
int result = 17;
if(fItem != null)
result = 31 * result + fItem.hashCode();
if(value != null)
result = 31 * result + value.hashCode();
if(rule != null)
result = 31 * result + rule.hashCode();
result = 31 * result + fId;
result = 31 * result + id;
return result;
}
}
他还建议我们编写单元测试以确保合同确实得到满足。这是他说的(斜体-粗体强调我的):
When you are finished writing your equals method, ask yourself three
questions: Is it symmetric? Is it transitive? Is it consistent? And
don’t just ask yourself; write unit tests to check that these
properties hold!
所以,我无法想象如何为那个非常简单的案例编写单元测试。好吧,我会写这样的东西:
public class MyTest{
//Each pair consists of the equals object
private Map<MyJavaBean, MyJavaBean> equalValueMap;
@Before
public void init(){
//initializing the map, using ThredLocalRandom
//to get ints and enums randomly
}
@Test
public void testReflexive(){
for(MyJavaBean fiv: equalValueMap.keySet()){
Assert.assertEquals(fiv.equals(fiv), true);
}
}
@Test
public void testSymmetric(){
for(MyJavaBean fiv: equalValueMap.keySet()){
Assert.assertEquals(equalValueMap.get(fiv).equals(fiv), true);
Assert.assertEquals(fiv.equals(equalValueMap.get(fiv)), true);
}
}
@Test
public void testHashCode(){
for(FilterItemValue fiv: equalFilterValueMap.keySet()){
Assert.assertEquals(equalFilterValueMap.get(fiv).hashCode(), fiv.hashCode());
}
}
}
但我认为这样的测试只是浪费构建时间,因为它们很简单。是否值得为简单 JavaBeans 的方法编写测试?
恕我直言!如果 equals()
和 hashCode()
的测试是由 IDE / 一些您信任的帮助库生成的,您可以安全地跳过它们。
尽管如此,如果您确实在做一些复杂的事情并且担心这些方法在运行时的行为是否符合预期,那么花时间编写一个简单的测试可能是值得的。
我正在阅读 J. Bloch 的有效 Java,需要复习 equals
/hashCode
合同和关系。
我有以下 JavaBeans class:
public class MyJavaBean{
private int id;
private Properties fItem; //Enumeration type
private String value;
private Rule rule; //Enumeration type
private int fId;
//GET, SET
@Override
public boolean equals(Object o){
if(!(o instanceof MyJavaBean))
return false;
MyJavaBeanv = (MyJavaBean) o;
return (fItem == null ? v.fItem == null : fItem.equals(v.fItem)) &&
(value == null ? v.value == null : value.equals(v.value)) &&
(rule == null ? v.rule == null : rule.equals(v.rule)) &&
fId == v.fId && id == v.id;
}
@Override
public int hashCode(){
int result = 17;
if(fItem != null)
result = 31 * result + fItem.hashCode();
if(value != null)
result = 31 * result + value.hashCode();
if(rule != null)
result = 31 * result + rule.hashCode();
result = 31 * result + fId;
result = 31 * result + id;
return result;
}
}
他还建议我们编写单元测试以确保合同确实得到满足。这是他说的(斜体-粗体强调我的):
When you are finished writing your equals method, ask yourself three questions: Is it symmetric? Is it transitive? Is it consistent? And don’t just ask yourself; write unit tests to check that these properties hold!
所以,我无法想象如何为那个非常简单的案例编写单元测试。好吧,我会写这样的东西:
public class MyTest{
//Each pair consists of the equals object
private Map<MyJavaBean, MyJavaBean> equalValueMap;
@Before
public void init(){
//initializing the map, using ThredLocalRandom
//to get ints and enums randomly
}
@Test
public void testReflexive(){
for(MyJavaBean fiv: equalValueMap.keySet()){
Assert.assertEquals(fiv.equals(fiv), true);
}
}
@Test
public void testSymmetric(){
for(MyJavaBean fiv: equalValueMap.keySet()){
Assert.assertEquals(equalValueMap.get(fiv).equals(fiv), true);
Assert.assertEquals(fiv.equals(equalValueMap.get(fiv)), true);
}
}
@Test
public void testHashCode(){
for(FilterItemValue fiv: equalFilterValueMap.keySet()){
Assert.assertEquals(equalFilterValueMap.get(fiv).hashCode(), fiv.hashCode());
}
}
}
但我认为这样的测试只是浪费构建时间,因为它们很简单。是否值得为简单 JavaBeans 的方法编写测试?
恕我直言!如果 equals()
和 hashCode()
的测试是由 IDE / 一些您信任的帮助库生成的,您可以安全地跳过它们。
尽管如此,如果您确实在做一些复杂的事情并且担心这些方法在运行时的行为是否符合预期,那么花时间编写一个简单的测试可能是值得的。