java.lang.AssertionError和两个实体是一样的吗?什么可能导致这个?

时间:2012-01-30 15:45:17

标签: java hibernate spring annotations hibernate-annotations

使用Assert.AssertEquals()方法测试DAO时出现以下错误:

java.lang.AssertionError: expected: com.develop.test.data.entity.Rpoint<com.develop.test.data.entity.Rpoint@7668e5b5> but was: com.develop.test.data.entity.Rpoint<com.develop.test.data.entity.Rpoint@7668e5b5>
at org.junit.Assert.fail(Assert.java:91)
at org.junit.Assert.failNotEquals(Assert.java:645)
at org.junit.Assert.assertEquals(Assert.java:126)
at org.junit.Assert.assertEquals(Assert.java:145)
at com.develop.test.data.dao.RpointDAOTest.testFindById(RpointDAOTest.java:80)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

以下是片段:

@Test
@Transactional(isolation = Isolation.SERIALIZABLE)
public void testFindById(){
    Category newCategory = new Category("C", "Recognize a Co-Worker", true);

    Rpoint newRPoint = new Rpoint.Builder()
                .id(3)
                .category(newCategory)                    
                .description("Maximize Resources")
                .points(50)
                .active(true)
                .build();

    Assert.assertEquals(newRPoint, this.RpointDao.findById(3));
}

我一直在使用调试,两个实体都有相同的数据。这可能是由实体中的equals / HashCode实现引起的吗?

编辑:添加了equals和hashcode:

@Override
public boolean equals(Object instance) {
    if (instance == null)
        return false;

    if (!(instance instanceof Rpoint))
        return false;

    Rpoint o = (Rpoint) instance;
    return new EqualsBuilder().append(this.getId(), o.getId()).
            append(this.getCategory(), o.getCategory()).
            append(this.getDescription(), o.getDescription()).
            append(this.getPoints(), o.getPoints()).
            append(this.getActive(), o.getActive()).
            isEquals();
}

@Override
public int hashCode() {
    return new HashCodeBuilder(23, 25).append(this.getId()).
            append(this.getCategory()).
            append(this.getDescription()).
            append(this.getPoints()).
            append(this.getActive()).
            toHashCode();
}

1 个答案:

答案 0 :(得分:6)

是的,如果equals被严重执行,即使您使用相同的引用它也会返回false,这肯定会导致失败。请注意,不仅仅是对象内的数据是相同的情况,但诊断表明它是双方的相同对象:

expected [...].Rpoint<com.develop.test.data.entity.Rpoint@7668e5b5>
but was: [...].Rpoint<com.develop.test.data.entity.Rpoint@7668e5b5>

7668e5b5不应该被视为对象标识,但它在这里非常具有启发性......

请显示您正在使用的equals方法。