[英]Java assertEquals causing failure in JUnit Testing
I am working on a java jdbcTemplate project in Netbeans and I am having trouble figuring out what is wrong with my equals and hashcode methods overriding the assertEquals test for my Dao.我正在研究 Netbeans 中的 java jdbcTemplate 项目,我无法弄清楚我的 equals 和哈希码方法覆盖了我的 Dao 的 assertEquals 测试有什么问题。 I was instucted that I need to do a "deep comparison" on the object, but from what I can see, my code is already doing that.
我被告知我需要对 object 进行“深度比较”,但据我所知,我的代码已经在这样做了。 Below are my different classes involving this issue
以下是涉及此问题的不同课程
Here is my Organization.class:这是我的 Organization.class:
public class Organization {
private int orgId;
private String orgName;
private String orgDescription;
private String orgEmail;
private String orgPhone;
private Location orgLocation;
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 getOrgEmail() {
return orgEmail;
}
public void setOrgEmail(String orgEmail) {
this.orgEmail = orgEmail;
}
public String getOrgPhone() {
return orgPhone;
}
public void setOrgPhone(String orgPhone) {
this.orgPhone = orgPhone;
}
public Location getOrgLocation() {
return orgLocation;
}
public void setOrgLocation(Location orgLocation) {
this.orgLocation = orgLocation;
}
@Override
public int hashCode() {
int hash = 7;
hash = 67 * hash + this.orgId;
hash = 67 * hash + Objects.hashCode(this.orgName);
hash = 67 * hash + Objects.hashCode(this.orgDescription);
hash = 67 * hash + Objects.hashCode(this.orgEmail);
hash = 67 * hash + Objects.hashCode(this.orgPhone);
hash = 67 * hash + Objects.hashCode(this.orgLocation);
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.orgEmail, other.orgEmail)) {
return false;
}
if (!Objects.equals(this.orgPhone, other.orgPhone)) {
return false;
}
if (!Objects.equals(this.orgLocation, other.orgLocation)) {
return false;
}
return true;
}
}
Here is my Organization Dao Implementation:这是我的组织道实现:
public class OrgDaoDBImpl implements OrgDao{
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
//ORGANIZATIONS - PREPARED STATEMENTS
private static final String SQL_INSERT_ORGANIZATION
= "insert into `organization` (org_name, org_description, org_email, "
+ "org_phone, location_id) "
+ "values (?, ?, ?, ?, ?)";
private static final String SQL_DELETE_ORGANIZATION
= "delete from `organization` where org_id = ?";
private static final String SQL_UPDATE_ORGANIZATION
= "update `organization` set org_name = ?, org_description = ?, org_email = ?, "
+ "org_phone = ?, location_id = ? "
+ "where org_id = ?";
private static final String SQL_SELECT_ORGANIZATION
= "select * from `organization` where org_id = ?";
private static final String SQL_SELECT_ALL_ORGANIZATIONS
= "select * from `organization`";
private static final String SQL_SELECT_LOCATION_BY_ORG_ID
= "select l.location_id, l.loc_name, l.loc_street_address, "
+ "l.loc_city, l.loc_state, l.loc_zip_code, l.loc_lat, l.loc_long "
+ "from location l "
+ "join organization o on l.location_id = o.location_id "
+ "where o.org_id = ?";
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void addOrganization(Organization organization) {
jdbcTemplate.update(SQL_INSERT_ORGANIZATION,
organization.getOrgName(),
organization.getOrgDescription(),
organization.getOrgPhone(),
organization.getOrgEmail(),
organization.getOrgLocation().getLocationId());
organization.setOrgId(
jdbcTemplate.queryForObject("select LAST_INSERT_ID()", Integer.class));
}
@Override
public void deleteOrganization(int organizationId) {
jdbcTemplate.update(SQL_DELETE_ORGANIZATION, organizationId);
}
@Override
public void updateOrganization(Organization organization) {
jdbcTemplate.update(SQL_UPDATE_ORGANIZATION,
organization.getOrgName(),
organization.getOrgDescription(),
organization.getOrgPhone(),
organization.getOrgEmail(),
organization.getOrgLocation().getLocationId());
}
@Override
public Organization getOrganizationById(int id) {
try {
Organization org = jdbcTemplate.queryForObject(SQL_SELECT_ORGANIZATION,
new OrgMapper(),
id);
org.setOrgLocation(findLocationForOrganization(org));
return org;
} catch (EmptyResultDataAccessException ex) {
return null;
}
}
@Override
public List<Organization> getAllOrganizations() {
return jdbcTemplate.query(SQL_SELECT_ALL_ORGANIZATIONS, new OrgMapper());
}
@Override
public List<Organization> getAllOrgsBySupeId(int supeId) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
/////////////////////
//*HELPER METHODS*//
//***************//
//FIND Location associated with an organization
private Location findLocationForOrganization(Organization org) {
return jdbcTemplate.queryForObject(SQL_SELECT_LOCATION_BY_ORG_ID,
new LocationMapper(),
org.getOrgId());
}
//ASSOCIATE the location with the org entry
private List<Organization>associateLocationWithOrg(List<Organization> orgList) {
// set the complete list of author ids for each book
for (Organization currentOrg : orgList) {
// add the Location to current Org
currentOrg.setOrgLocation(findLocationForOrganization(currentOrg)); }
return orgList;
}
/////////////
//*MAPPERS*/
//*******//
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"));
return org;
}
}
private static class LocationMapper implements RowMapper<Location>{
@Override
public Location mapRow(ResultSet rs, int i) throws SQLException {
Location loc = new Location();
loc.setLocationId(rs.getInt("location_id"));
loc.setLocName(rs.getString("loc_name"));
loc.setLocStreetAddress(rs.getString("loc_street_address"));
loc.setLocCity(rs.getString("loc_city"));
loc.setLocState(rs.getString("loc_state"));
loc.setLocZipCode(rs.getString("loc_zip_code"));
loc.setLocLat(rs.getString("loc_lat"));
loc.setLocLong(rs.getString("loc_long"));
return loc;
}
}
}
My Organization.class uses the Location object to input additional location information. My Organization.class 使用位置 object 输入附加位置信息。 So below, I first have to add a location before attempting to add an organization.
因此,在下面,我首先必须添加一个位置,然后再尝试添加一个组织。 At current, the test for adding a location is successful.
目前,添加位置测试成功。
public class SuperSighting_DaoTests {
LocationDao ldao;
OrgDao odao;
PowerDao pdao;
SightingDao sidao;
SupeDao sudao;
public SuperSighting_DaoTests() {
}
@BeforeClass
public static void setUpClass() {
}
@AfterClass
public static void tearDownClass() {
}
@Before
public void setUp() {
ApplicationContext ctx
= new ClassPathXmlApplicationContext("test-applicationContext.xml");
ldao = ctx.getBean("LocationDao", LocationDao.class);
odao = ctx.getBean("OrgDao", OrgDao.class);
pdao = ctx.getBean("PowerDao", PowerDao.class);
sidao = ctx.getBean("SightingDao", SightingDao.class);
sudao = ctx.getBean("SupeDao", SupeDao.class);
// delete all supes
List<Supe> supes = sudao.getAllSupes(); for (Supe currentSupe : supes) {
sudao.deleteSupe(currentSupe.getSupeId());
}
// delete all powers
List<Power> powers = pdao.getAllPowers(); for (Power currentPower : powers) {
pdao.deletePower(currentPower.getPowerId());
}
//delete all organizations
List<Organization> orgs = odao.getAllOrganizations(); for (Organization currentOrg : orgs) {
odao.deleteOrganization(currentOrg.getOrgId());
}
// delete all locations
List<Location> locations = ldao.getAllLocations(); for (Location currentLocation : locations) {
ldao.deleteLocation(currentLocation.getLocationId());
}
// delete all sightings
List<Sighting> sightings = sidao.getAllSightings(); for (Sighting currentSighting : sightings) {
sidao.deleteSighting(currentSighting.getSightingId());
}
}
@Test
public void testAddGetOrganization() {
Location loc = new Location();
loc.setLocName("Legion of Doom");
loc.setLocStreetAddress("127 Taco St.");
loc.setLocCity("Smalltown");
loc.setLocState("MA");
loc.setLocZipCode("19698");
loc.setLocLat("39.16567925978815");
loc.setLocLong("-75.59452746539126");
ldao.addLocation(loc);
Organization org = new Organization();
org.setOrgName("Legion of Doom");
org.setOrgDescription("evil organization");
org.setOrgPhone("333-444-5678");
org.setOrgEmail("lod@evil.org");
org.setOrgLocation(loc);
odao.addOrganization(org);
Organization fromDao = odao.getOrganizationById(org.getOrgId());
fromDao.toString();
assertEquals(fromDao, org);
}
When the test completes, I can see that the compared items are exactly the same, but the test fails and gives me this error:测试完成后,我可以看到比较的项目完全相同,但测试失败并给我这个错误:
Tests run: 8, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 2.931 sec <<< FAILURE.
测试运行:8,失败:1,错误:0,跳过:0,经过的时间:2.931 秒 <<< 失败。 testAddGetOrganization(com.sg.supersightingsv2.dao:SuperSighting_DaoTests) Time elapsed.
testAddGetOrganization(com.sg.supersightingsv2.dao:SuperSighting_DaoTests) 时间已过。 0.177 sec <<< FAILURE.
0.177 秒 <<< 失败。 java:lang:AssertionError.
java:lang:AssertionError。 expected.com.sg.supersightingsv2:model.Organization@ad172ff9 but was.com.sg.supersightingsv2.model.Organization@e8c8a831 at org.junit.Assert:fail(Assert.java:88)
expected.com.sg.supersightingsv2:model.Organization@ad172ff9 but was.com.sg.supersightingsv2.model.Organization@e8c8a831 at org.junit.Assert:fail(Assert.java:88)
Here is the image showing the field values for both objects ('org' and 'fromDao') being compared: two object being compared这是显示正在比较的两个对象('org' 和 'fromDao')的字段值的图像:两个 object 正在比较
I am just really unsure where to go from here because my end goal relies on the test passing.我真的不确定 go 从这里到哪里,因为我的最终目标取决于测试通过。 I have gone through the code many many times, and I am at a lost for what to do other that rewriting the whole program.
我已经多次阅读了代码,除了重写整个程序之外,我不知道该怎么做。 Any suggestions are appreciated as I am still new to this!
任何建议都值得赞赏,因为我还是新手! Thank you!
谢谢!
deleted: totally wrong - should have checked screenshots with more care已删除:完全错误 - 应该更仔细地检查屏幕截图
I wanted to delete this question but I can't.我想删除这个问题,但我不能。 -thanks for the quick reponses!
- 感谢您的快速回复!
I somehow fixed this issue... It actually turned out that my phone and email entires were getting switched on retrieval -you can see this in the screenshot...its easy to miss after hours of staring.我以某种方式解决了这个问题......实际上我的手机和 email 正在开启检索 - 你可以在屏幕截图中看到这个......经过数小时的凝视后很容易错过。
I went through all my code and switched the order to match the database and my tests are al green now.我浏览了所有代码并切换了顺序以匹配数据库,我的测试现在都是绿色的。
Thanks again!再次感谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.