Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.AssertionError和这两个实体是否相同?这是什么原因造成的?_Java_Hibernate_Spring_Annotations_Hibernate Annotations - Fatal编程技术网

java.lang.AssertionError和这两个实体是否相同?这是什么原因造成的?

java.lang.AssertionError和这两个实体是否相同?这是什么原因造成的?,java,hibernate,spring,annotations,hibernate-annotations,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@7

使用
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)
我一直在使用debug,两个实体都有相同的数据。这可能是由实体中的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();
}

是的,如果
equals
实现得不好,即使使用相同的引用,它也会返回false,这肯定会导致失败。请注意,这不仅仅是对象内的数据相同的情况,而且诊断结果表明两侧都是相同的对象:

expected [...].Rpoint<com.develop.test.data.entity.Rpoint@7668e5b5>
but was: [...].Rpoint<com.develop.test.data.entity.Rpoint@7668e5b5>
应为[…].Rpoint
但是是:[……]Rpoint
7668e5b5实际上不应该被视为对象标识,但它在这里非常具有启发性


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

Rpoint的equals/hashCode是什么样子的?我想我在类别equals
中发现了错误,如果(!(类别实例instanceof category))返回false已设置为其他实体。我修复了这个,现在它工作了。我讨厌复制/粘贴的人:(
expected [...].Rpoint<com.develop.test.data.entity.Rpoint@7668e5b5>
but was: [...].Rpoint<com.develop.test.data.entity.Rpoint@7668e5b5>