Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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 AssertionError:一个方法测试正确,但一个类似的方法测试不正确_Java_Hibernate_Spring_Hibernate Annotations - Fatal编程技术网

Java AssertionError:一个方法测试正确,但一个类似的方法测试不正确

Java AssertionError:一个方法测试正确,但一个类似的方法测试不正确,java,hibernate,spring,hibernate-annotations,Java,Hibernate,Spring,Hibernate Annotations,我正在从DAO测试以下两种方法: public List<T> findAll() { if (logger.isDebugEnabled()) { logger.debug("findAll"); } return currentSession().createCriteria(getPersistentClass()).list(); } public int count() { if (logger.isDebugEnabled

我正在从DAO测试以下两种方法:

public List<T> findAll() {
    if (logger.isDebugEnabled()) {
        logger.debug("findAll");
    }
    return currentSession().createCriteria(getPersistentClass()).list();
}

public int count() {
    if (logger.isDebugEnabled()) {
        logger.debug("count");
    }
    return ((Number) currentSession().createCriteria(getPersistentClass()).
            setProjection(Projections.rowCount()).
            uniqueResult()).intValue();
}
表上总共有2566个条目,
count
成功返回其计数,但是
findAll
没有

错误:

java.lang.AssertionError: 
Expected :2566
Actual   :0
    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:470)
    at org.junit.Assert.assertEquals(Assert.java:454)
    at com.develop.test.data.dao.EmployeeDAOTest.testFindAll(EmployeeDAOTest.java:53)
    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)
因此,给定该错误,它实际上在该特定方法上返回0。我不明白为什么考虑计数是成功的。有什么帮助吗

编辑: 用于计数()的SQL:

用于
findAll()
的SQL(employee3指的是更新的人字段):


难道不可能为联接查询实际提供零行吗?加入
首选项
是否成功?

您是否尝试启用已执行SQL的日志记录?(hibernate.show_sql)@PeterLiljenberg我发布了我从日志中看到的编辑内容。似乎
内部连接首选项
一定是问题所在。不知何故,您一定已经说服了Hibernate,每个
员工
都至少有一个
首选项
,而事实上没有
员工
这样做。(对吗?@ruakh感谢您的帮助,我将David Grant的答案标记为答案,尽管您也提供了帮助。好吧,我似乎已经确定了一些最有可能(但可能不完全)导致此问题的问题。我的首选项表没有填充,我宁愿忽略它,因为默认情况下它应该设置为一个设置。另一个问题是,它当然在updatedBy字段中将自己链接到EntityEmployee。因此,我不确定如何继续,所以暂时忽略它,或者简单地用employee表中每个条目的默认值填充它-这意味着preference将有2566个条目,因为它与employee表是一对一的。我现在只是懒洋洋地加载了preference,谢谢。我把它标为答案。
java.lang.AssertionError: 
Expected :2566
Actual   :0
    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:470)
    at org.junit.Assert.assertEquals(Assert.java:454)
    at com.develop.test.data.dao.EmployeeDAOTest.testFindAll(EmployeeDAOTest.java:53)
    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)
09:56:15.180 [main] DEBUG org.hibernate.SQL - 
    select
        count(*) as y0_ 
    from
        EMPLOYEE this_
Hibernate: 
    select
        count(*) as y0_ 
    from
        EMPLOYEE this_
09:56:15.243 [main] DEBUG org.hibernate.SQL - 
    select
        this_.EMP_ID as EMP1_5_2_,
        this_.ISACTIVE as ISACTIVE5_2_,
        this_.COMPANY_CODE as COMPANY12_5_2_,
        this_.CORP_TITLE as CORP3_5_2_,
        this_.DEPT_CODE as DEPT13_5_2_,
        this_.E_MAIL as E4_5_2_,
        this_.EMP_FIRST_NAME as EMP5_5_2_,
        this_.HIRE_DATE as HIRE6_5_2_,
        this_.JOB_CODE as JOB7_5_2_,
        this_.JOB_TITLE_DESC as JOB8_5_2_,
        this_.EMP_LAST_NAME as EMP9_5_2_,
        this_.MANAGER_ID as MANAGER14_5_2_,
        this_.MANAGER_INDC as MANAGER10_5_2_,
        this_.EMP_MIDDLE_NAME as EMP11_5_2_,
        preference2_.EMP_ID as EMP1_13_0_,
        preference2_.UPDATED_BY as UPDATED4_13_0_,
        preference2_.UPDATED_DATE as UPDATED2_13_0_,
        preference2_.LANGUAGE as LANGUAGE13_0_,
        employee3_.EMP_ID as EMP1_5_1_,
        employee3_.ISACTIVE as ISACTIVE5_1_,
        employee3_.COMPANY_CODE as COMPANY12_5_1_,
        employee3_.CORP_TITLE as CORP3_5_1_,
        employee3_.DEPT_CODE as DEPT13_5_1_,
        employee3_.E_MAIL as E4_5_1_,
        employee3_.EMP_FIRST_NAME as EMP5_5_1_,
        employee3_.HIRE_DATE as HIRE6_5_1_,
        employee3_.JOB_CODE as JOB7_5_1_,
        employee3_.JOB_TITLE_DESC as JOB8_5_1_,
        employee3_.EMP_LAST_NAME as EMP9_5_1_,
        employee3_.MANAGER_ID as MANAGER14_5_1_,
        employee3_.MANAGER_INDC as MANAGER10_5_1_,
        employee3_.EMP_MIDDLE_NAME as EMP11_5_1_ 
    from
        EMPLOYEE this_ 
    inner join
        PREFERENCE preference2_ 
            on this_.EMP_ID=preference2_.EMP_ID 
    left outer join
        EMPLOYEE employee3_ 
            on preference2_.UPDATED_BY=employee3_.EMP_ID
Hibernate: 
    select
        this_.EMP_ID as EMP1_5_2_,
        this_.ISACTIVE as ISACTIVE5_2_,
        this_.COMPANY_CODE as COMPANY12_5_2_,
        this_.CORP_TITLE as CORP3_5_2_,
        this_.DEPT_CODE as DEPT13_5_2_,
        this_.E_MAIL as E4_5_2_,
        this_.EMP_FIRST_NAME as EMP5_5_2_,
        this_.HIRE_DATE as HIRE6_5_2_,
        this_.JOB_CODE as JOB7_5_2_,
        this_.JOB_TITLE_DESC as JOB8_5_2_,
        this_.EMP_LAST_NAME as EMP9_5_2_,
        this_.MANAGER_ID as MANAGER14_5_2_,
        this_.MANAGER_INDC as MANAGER10_5_2_,
        this_.EMP_MIDDLE_NAME as EMP11_5_2_,
        preference2_.EMP_ID as EMP1_13_0_,
        preference2_.UPDATED_BY as UPDATED4_13_0_,
        preference2_.UPDATED_DATE as UPDATED2_13_0_,
        preference2_.LANGUAGE as LANGUAGE13_0_,
        employee3_.EMP_ID as EMP1_5_1_,
        employee3_.ISACTIVE as ISACTIVE5_1_,
        employee3_.COMPANY_CODE as COMPANY12_5_1_,
        employee3_.CORP_TITLE as CORP3_5_1_,
        employee3_.DEPT_CODE as DEPT13_5_1_,
        employee3_.E_MAIL as E4_5_1_,
        employee3_.EMP_FIRST_NAME as EMP5_5_1_,
        employee3_.HIRE_DATE as HIRE6_5_1_,
        employee3_.JOB_CODE as JOB7_5_1_,
        employee3_.JOB_TITLE_DESC as JOB8_5_1_,
        employee3_.EMP_LAST_NAME as EMP9_5_1_,
        employee3_.MANAGER_ID as MANAGER14_5_1_,
        employee3_.MANAGER_INDC as MANAGER10_5_1_,
        employee3_.EMP_MIDDLE_NAME as EMP11_5_1_ 
    from
        EMPLOYEE this_ 
    inner join
        PREFERENCE preference2_ 
            on this_.EMP_ID=preference2_.EMP_ID 
    left outer join
        EMPLOYEE employee3_ 
            on preference2_.UPDATED_BY=employee3_.EMP_ID