Java 如何在 JUNIT 测试中使用 assertEquals 或任何其他方式比较两个对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22301806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compare two objects using assertEquals or by any other way in JUNIT testing?
提问by Amir
Below is JUNIT test which I have written to compare the object created (Actual) from the Json string and the object created (Expected) within the test function.
下面是我编写的 JUNIT 测试,用于比较从 Json 字符串创建的对象(实际)和在测试函数中创建的对象(预期)。
@Test
public void testSalesChannel_1_ObjSrc(){
SalesChannel oScExpected = new SalesChannel();
oScExpected.setSalesChannelId(79662);
oScExpected.setCompanyName("BMW");
oScExpected.setCountry("DE");
oScExpected.setActive(true);
String jsonStringActual = "{\"salesChannelId\":79662,"
+ "\"companyName\":\"BMW\","
+ "\"country\":\"DE\",\"isActive\":true,"
+ "\"salesChannelContact\":[]}";
SalesChannel oScActual = gson.fromJson(jsonStringActual, SalesChannel.class);
System.out.println(oScExpected.toString());
System.out.println(oScActual.toString());
//assertEquals(oScExpected, oScActual);
assertTrue(oScExpected.equals(oScActual));
}
BUT when I execute assertEquals(), it fails the test. What could be the reason?
但是当我执行 assertEquals() 时,它没有通过测试。可能是什么原因?
AND my Sales Channel class is:
我的销售渠道课程是:
package com.pf.activationServer;
import java.util.List;
import com.pf.activationServer.SalesChannelContact;
public class SalesChannel {
private int salesChannelId;
private String companyName;
private CountryCode country;
private boolean isActive;
private List<SalesChannelContact> salesChannelContact;
// getter methods
protected int getSalesChannelId() {
return salesChannelId;
}
protected String getCompanyName() {
return companyName;
}
protected CountryCode getCountry() {
return country;
}
protected boolean isActive() {
return isActive;
}
protected List<SalesChannelContact> getSalesChannelContact() {
return salesChannelContact;
}
// setter methods
protected void setSalesChannelId(int salesChannelId) {
this.salesChannelId = salesChannelId;
}
protected void setCompanyName(String companyName) {
this.companyName = companyName;
}
protected void setCountry(String a){
this.country = CountryCode.valueOf(a);
}
protected void setActive(boolean isActive) {
this.isActive = isActive;
}
protected void setSalesChannelContact(List<SalesChannelContact> salesChannelContact) {
this.salesChannelContact = salesChannelContact;
}
// Checks whether two SalesChannel objects are equal or not
public boolean equals(SalesChannel other) {
if (this == other)
return true;
if (other == null)
return false;
if (getClass() != other.getClass())
return false;
//SalesChannel other = (SalesChannel) obj;
if (companyName == null) {
if (other.companyName != null)
return false;
} else if (!companyName.equals(other.companyName))
return false;
if (country != other.country)
return false;
if (isActive != other.isActive)
return false;
if (this.salesChannelContact == null) {
if (other.salesChannelContact != null)
return false;
} else if (!salesChannelContact.equals(other.salesChannelContact))
return false;
if (salesChannelId != other.salesChannelId)
return false;
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((companyName == null) ? 0 : companyName.hashCode());
result = prime * result + ((country == null) ? 0 : country.hashCode());
result = prime * result + (isActive ? 1231 : 1237);
result = prime
* result
+ ((salesChannelContact == null) ? 0 : salesChannelContact
.hashCode());
result = prime * result + salesChannelId;
return result;
}
} // END class SalesChannel
采纳答案by ruhungry
@Test
public void testMyClass(){
MyClass objExpected = new MyClass();
objExpected.setMyClasslId(79662);
objExpected.setMyClassCompanyName("BMW");
objExpected.setCountry("DE");
objExpected.setActive(true);
String jsonStringActual = "{\"myClassId\":79662,"
+ "\"myClasscompanyName\":\"BMW\","
+ "\"country\":\"DE\",\"isActive\":true,"
+ "\"MyClassContacts\":[]}";
MyClass objcActual = gson.fromJson(jsonStringActual, MyClass.class);
System.out.println(objExpected.toString());
System.out.println(objActual.toString());
assertEquals(objExpected, objActual);
}
Run this code and paste your input
运行此代码并粘贴您的输入
回答by user3152555
Check your variable names and types in POJO Myclass as GSON recommends strong name and type checking of class relative to JSON.
在 POJO Myclass 中检查您的变量名称和类型,因为 GSON 建议相对于 JSON 的类进行强名称和类型检查。
回答by sweetfa
Your equals method is using a specific class and not Object.
您的 equals 方法使用的是特定的类而不是 Object。
You need to override the equals(Object obj) method with your own implementation.
您需要使用自己的实现覆盖 equals(Object obj) 方法。
If in that you first perform typechecking, and then call your implemented equals class you should have more luck.
如果你首先执行类型检查,然后调用你实现的 equals 类,你应该有更多的运气。
assertEquals is calling the equals(Object obj) method of the super class (in your case Object) which by default just compares the reference of the object. As they are not the same object your result will fail.
assertEquals 正在调用超类(在您的情况下为 Object)的 equals(Object obj) 方法,默认情况下它只比较对象的引用。由于它们不是同一个对象,因此您的结果将失败。
So if you look at it as the interpreter sees it.
因此,如果您像解释器那样看待它。
public class SalesChannel extends Object
{
}
public class Object
{
public boolean equals(Object obj)
{
return this == obj;
}
}
So to fix it
所以要修复它
public class SalesChannel
{
@Override public boolean equals(Object obj)
{
if (obj != null && obj.isInstance(SalesChannel.class))
return this.equals((SalesChannel) obj);
return false;
}
}
回答by Stanislav Bashkyrtsev
Answering the question in title: in order to compare fields of 2 objects use Unitils and its assertReflectionEquals.
回答标题中的问题:为了比较 2 个对象的字段,请使用 Unitils 及其assertReflectionEquals。

