gpt4 book ai didi

java - 如何在 JUNIT 测试中使用 assertEquals 或任何其他方式比较两个对象?

转载 作者:搜寻专家 更新时间:2023-10-31 20:20:26 25 4
gpt4 key购买 nike

下面是我编写的 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));
}

但是当我执行 assertEquals() 时,它没有通过测试。可能是什么原因?

我的销售 channel 类是:

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

最佳答案

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

运行此代码并粘贴您的输入

关于java - 如何在 JUNIT 测试中使用 assertEquals 或任何其他方式比较两个对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22301806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com