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

Trying to figure out unexplained error with JUnit tests and model object

我正在对几个 java 对象建模,这些对象使用 JDBCTemplate 在 mySQL 数据库中管理实体。

我对另外两个对象进行了 运行 Add/Get JUnit 测试,我没有收到任何错误,但我无法弄清楚是什么导致了我的 'Organization' 对象出现此错误。

这是我的 '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;
    }

   

   
    
    
}

这是我的 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) Time elapsed: 0.107 sec <<< FAILURE! java.lang.AssertionError: expected:com.sg.superherosightings.model.Organization@ae511546 but was:com.sg.superherosightings.model.Organization@15fabf0f

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

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

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

谢谢大家的帮助!我能够将我的 org 和 fromDao 对象转换为字符串,以便在测试中看到它们 window。问题出在我的 Organization 对象的 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;
        
        }