我正在使用jdbctemplate为几个java对象建模,这些对象管理mysql数据库中的实体。
我已经在另外两个对象上运行了add/getjunit测试,没有发现任何错误,但是我无法找出是什么导致了我的“organization”对象出现此错误。
以下是我的“组织”dto代码:
package com.sg.superherosightings.model;
import java.util.Objects;
public class Organization {
private int orgId;
private String orgName;
private String orgDescription;
private String orgPhone;
private String orgEmail;
private String orgStreetAddress;
private String orgCity;
private String orgState;
private String orgZipCode;
public int getOrgId() {
return orgId;
}
public void setOrgId(int orgId) {
this.orgId = orgId;
}
public String getOrgName() {
return orgName;
}
public void setOrgName(String orgName) {
this.orgName = orgName;
}
public String getOrgDescription() {
return orgDescription;
}
public void setOrgDescription(String orgDescription) {
this.orgDescription = orgDescription;
}
public String getOrgPhone() {
return orgPhone;
}
public void setOrgPhone(String orgPhone) {
this.orgPhone = orgPhone;
}
public String getOrgEmail() {
return orgEmail;
}
public void setOrgEmail(String orgEmail) {
this.orgEmail = orgEmail;
}
public String getOrgStreetAddress() {
return orgStreetAddress;
}
public void setOrgStreetAddress(String orgStreetAddress) {
this.orgStreetAddress = orgStreetAddress;
}
public String getOrgCity() {
return orgCity;
}
public void setOrgCity(String orgCity) {
this.orgCity = orgCity;
}
public String getOrgState() {
return orgState;
}
public void setOrgState(String orgState) {
this.orgState = orgState;
}
public String getOrgZipCode() {
return orgZipCode;
}
public void setOrgZipCode(String orgZipCode) {
this.orgZipCode = orgZipCode;
}
@Override
public int hashCode() {
int hash = 7;
hash = 89 * hash + this.orgId;
hash = 89 * hash + Objects.hashCode(this.orgName);
hash = 89 * hash + Objects.hashCode(this.orgDescription);
hash = 89 * hash + Objects.hashCode(this.orgPhone);
hash = 89 * hash + Objects.hashCode(this.orgEmail);
hash = 89 * hash + Objects.hashCode(this.orgStreetAddress);
hash = 89 * hash + Objects.hashCode(this.orgCity);
hash = 89 * hash + Objects.hashCode(this.orgState);
hash = 89 * hash + Objects.hashCode(this.orgZipCode);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Organization other = (Organization) obj;
if (this.orgId != other.orgId) {
return false;
}
if (!Objects.equals(this.orgName, other.orgName)) {
return false;
}
if (!Objects.equals(this.orgDescription, other.orgDescription)) {
return false;
}
if (!Objects.equals(this.orgPhone, other.orgPhone)) {
return false;
}
if (!Objects.equals(this.orgEmail, other.orgEmail)) {
return false;
}
if (!Objects.equals(this.orgStreetAddress, other.orgStreetAddress)) {
return false;
}
if (!Objects.equals(this.orgCity, other.orgCity)) {
return false;
}
if (!Objects.equals(this.orgState, other.orgState)) {
return false;
}
if (!Objects.equals(this.orgZipCode, other.orgZipCode)) {
return false;
}
return true;
}
}
这是我的Map绘制方法在我的daodbimpl:img of 修复前的组织Map器方法
这是我的SuperSighings测试方法导致错误:
package com.sg.superherosightings.dao;
import com.sg.superherosightings.model.Location;
import com.sg.superherosightings.model.Organization;
import com.sg.superherosightings.model.Power;
import com.sg.superherosightings.model.Sighting;
import com.sg.superherosightings.model.Supe;
import java.util.List;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SuperSightings_DaoTest {
SuperSightings_Dao dao;
public SuperSightings_DaoTest() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
ApplicationContext ctx
= new ClassPathXmlApplicationContext("test-applicationContext.xml");
dao = ctx.getBean("SuperSightings_Dao", SuperSightings_Dao.class);
// delete all supes
List<Supe> supes = dao.getAllSupes(); for (Supe currentSupe : supes) {
dao.deleteSupe(currentSupe.getSupeId());
}
// delete all powers
List<Power> powers = dao.getAllPowers(); for (Power currentPower : powers) {
dao.deletePower(currentPower.getPowerId());
}
//delete all organizations
List<Organization> orgs = dao.getAllOrganizations(); for (Organization currentOrg : orgs) {
dao.deleteOrganization(currentOrg.getOrgId());
}
// delete all locations
List<Location> locations = dao.getAllLocations(); for (Location currentLocation : locations) {
dao.deleteLocation(currentLocation.getLocationId());
}
// delete all sightings
List<Sighting> sightings = dao.getAllSightings(); for (Sighting currentSighting : sightings) {
dao.deleteSighting(currentSighting.getSightingId());
}
}
@After
public void tearDown() {
}
/**
* Test of addPower method, of class SuperSightings_Dao.
*/
@Test
public void testAddGetPower() {
Power power = new Power();
power.setPowerType("Fire");
power.setPowerDescription("Shoots fire from hands");
dao.addPower(power);
Power fromDao = dao.getPowerById(power.getPowerId());
assertEquals(fromDao, power);
}
/**
* Test of deletePower method, of class SuperSightings_Dao.
*/
@Test
public void testDeletePower() {
Power power = new Power();
power.setPowerType("Fire");
power.setPowerDescription("Shoots fire from hands");
dao.addPower(power);
Power fromDao = dao.getPowerById(power.getPowerId());
assertEquals(fromDao, power);
dao.deletePower(power.getPowerId());
assertNull(dao.getPowerById(power.getPowerId()));
}
/**
* Test of getAllPowersBySupeId method, of class SuperSightings_Dao.
*/
@Test
public void testGetAllPowersBySupeId() {
}
/**
* Test of addOrganization method, of class SuperSightings_Dao.
*/
@Test
public void testAddGetOrganization() {
Organization org = new Organization();
org.setOrgName("Legion of Doom");
org.setOrgDescription("evil organization");
org.setOrgPhone("333-444-5678");
org.setOrgEmail("lod@evil.org");
org.setOrgStreetAddress("344 Lowland Blvd.");
org.setOrgCity("Quahog");
org.setOrgState("RI");
org.setOrgZipCode("09678");
dao.addOrganization(org);
Organization fromDao = dao.getOrganizationById(org.getOrgId());
assertEquals(fromDao, org); //this is the line causing the error
}
这是我得到的错误:
testaddgetorganization(com.sg.superherosightings.dao.supershightings\u daotest)运行时间:0.107秒<<<失败!java.lang.assertionerror错误:expected:com.sg.superherosightings.model.organization@ae511546 但是was:com.sg.superherosightings.model.organization@15fabf0f
请让我知道如果我需要提供进一步的信息。我正在努力提高我在这里张贴问题的方式。在询问之前,我搜索了很长时间,但我能找到的只是它可能与我的equals/哈希代码有关。我只是不确定在进行比较时会发生什么变化,因为它不会发生在我的其他对象上。
谢谢你的任何提示,请不要咬我的头!
2条答案
按热度按时间5fjcxozz1#
谢谢大家的帮助!我能够将我的org和fromdao对象转换为string,以便在测试窗口中看到它们。问题出在组织对象的Map器方法上。请参阅以下原始版本和修复程序:
原始版本
固定组织Map器:
gblwokeq2#
似乎有些领域是不平等的。尝试逐个比较所有字段以确定不相等的字段:assertequals(fromdao.getorgid()、org.getorgid()和组织的所有其他字段)