试图通过 JUnit 测试和模型对象找出无法解释的错误

问题描述

我正在对使用 JDBCTemplate 管理 MysqL 数据库中的实体的多个 Java 对象进行建模。

我已经在另外两个对象上运行了 Add/Get JUnit 测试,我没有收到任何错误,但我无法弄清楚是什么导致了我的“组织”对象出现此错误

这是我的“组织”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;
    }

   

   
    
    
}

这是我在 DaoDBImpl 中的 Mapper 方法 img of OrgMapper Method before fix

这是我的 SuperSightings_DaoTest 方法导致错误

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.SuperSightings_DaoTest) 已用时间:0.107 秒

如果我需要提供更多信息,请告诉我。我正在努力改进我在这里发布问题的方式。在询问之前我搜索了很长时间,但我能找到的只是它可能与我的 equals/hash 代码有关。我只是不确定进行比较时会发生什么变化,因为我的其他对象没有发生这种情况。

谢谢你的任何提示,请不要咬我的头!

解决方法

似乎有些字段不相等。尝试将所有字段一一比较以识别不相等的字段: assertEquals(fromDao.getOrgId(),org.getOrgId() 和组织的所有其余字段)

,

感谢大家的帮助!我能够将我的 org 和 fromDao 对象转换为字符串以在测试窗口中查看它们。问题出在我的组织对象的 Mapper 方法上。请参阅下面的原始和修复程序:

原始版本

    private static final class OrgMapper implements RowMapper<Organization> {
        
        @Override
        public Organization mapRow(ResultSet rs,int i) throws SQLException {
            Organization org = new Organization();
            org.setOrgId(rs.getInt("org_id"));
            org.setOrgName(rs.getString("org_name"));
            org.setOrgDescription(rs.getString("org_description"));
            org.setOrgPhone(rs.getString("org_phone"));
            org.setOrgEmail(rs.getString("org_street_address")); //wrong field
            org.setOrgCity(rs.getString("org_city"));
            org.setOrgState(rs.getString("org_state"));
            org.setOrgZipCode(rs.getString("org_zip_code"));
            
            return org;
        
        }    
    }
    

固定 OrgMapper:

    private static final class OrgMapper implements RowMapper<Organization> {
        
        @Override
        public Organization mapRow(ResultSet rs,int i) throws SQLException {
            Organization org = new Organization();
            org.setOrgId(rs.getInt("org_id"));
            org.setOrgName(rs.getString("org_name"));
            org.setOrgDescription(rs.getString("org_description"));
            org.setOrgPhone(rs.getString("org_phone"));
            org.setOrgEmail(rs.getString("org_email"));
            org.setOrgStreetAddress(rs.getString("org_street_address"));
            org.setOrgCity(rs.getString("org_city"));
            org.setOrgState(rs.getString("org_state"));
            org.setOrgZipCode(rs.getString("org_zip_code"));
            
            return org;
        
        }