Home  >  Article  >  Java  >  JUnit assertEquals two object or collection types

JUnit assertEquals two object or collection types

黄舟
黄舟Original
2016-12-28 11:43:271606browse

对于两个自定义类型MyObjClass对象,使用JUnit时,是否可以用下面的方式判断它们的内容相同呢?

assertEquals(obj1, obj2)

答案是:如果MyObjClass类重载了下面的函数,则是可以的。否则不可预期。

@Overrid  
public boolean equals(Object other)

同理,是否可以这样比较两个Map内容是否相同呢?


assertEquals(map1, map2)

答案是:如果Map中的对象的类重载了上面的函数,则是可以的。否则不可预期。


比如Dog类重载equals() 方法如下:

public class Dog {  
    public int age;  
      
    public boolean equals(Object o) {  
        if (o instanceof Dog) {  
            return (age == o.age);  
        }  
          
        return false;  
    }  
}

 以上就是JUnit assertEquals 两个对象或集合类型的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn