JUnit返回错误创建bean,名称为' sessionFactory'在URL中定义

时间:2015-09-09 02:05:45

标签: java spring hibernate spring-mvc junit

我是JUnit的新手。为了实验它,我想测试一个Spring的存储库类。它只有一个方法用于根据提供的id检索行,但我收到一个异常。我发现解决问题的一种方法是添加一个上下文类,但我不确定这是否有帮助,如果有的话,不确定如何做到这一点。另一个问题是,当我想运行测试用例时,我需要运行该项目吗?!!

异常

 java.lang.IllegalStateException: Failed to load ApplicationContext
            at org.springframework.test.context.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:94)
            at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:72)
            at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(
            .........
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Table1RepositoryImpl': 
Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not 
autowire field: private org.hibernate.SessionFactory com.myproject.repository.Table1RepositoryImpl.sessionFactory; nested 
exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL 
[file:WebContent/WEB-INF/my-servlet.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: 
javax.persistence.OneToMany.orphanRemoval()Z
        at 
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(
        ....
    Caused by: org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: private org.hibernate.SessionFactory 
com.myproject.repository.Table1RepositoryImpl.sessionFactory; nested 
exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory' defined in URL [file:WebContent/WEB-INF/my-servlet.xml]: Invocation of init method 
failed; nested exception is java.lang.NoSuchMethodError:
javax.persistence.OneToMany.orphanRemoval()Z
    ....
    Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sessionFactory' defined in URL 
[file:WebContent/WEB-INF/my-servlet.xml]: Invocation of init method 
failed; nested exception is java.lang.NoSuchMethodError: 
javax.persistence.OneToMany.orphanRemoval()Z
        ......
    Caused by: java.lang.NoSuchMethodError: javax.persistence.OneToMany.orphanRemoval()Z
        at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:2021)
        at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:895)
        at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:728)
        at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3625)
        at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3579)
        at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1381)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1786)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
        at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1625)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1562)
        ... 53 more

JUnit测试用例

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"file:WebContent/WEB-INF/my-servlet.xml"})  
public class Table1RepositoryTests {

    @Autowired
    private Table1Repository table1Repository;

    private Table1 table1;

    @Before
    public void setup(){
        table1 = new Table1();
        table1.setDescription("This is row 1 of table 1");
        table1.setId(1);
    }

    @Test
    public void shouldReturnFirstRowOfTable1(){
        Table1 result = table1Repository.retrieveTableById(1);
        assertEquals(result,table1);
    }

}

更新

好像我应该注入SessionFactory对象。因为我在我的存储库类中有以下行。

    @Autowired
    private SessionFactory sessionFactory;

此外,我发现在link后面有"例如,不要编写发出HTTP请求,访问数据库或从文件系统读取的单元测试。&# 34; 那么我应该如何测试我的存储库代码是否正常工作?

1 个答案:

答案 0 :(得分:0)

  1. 您可以使用HSQLDB来测试存储库代码。 HSQLDB可以在内存模式下使用,并在每次运行UnitTest时重新构建。
  2. 恕我直言,建议只测试各个班级。所以你最好模拟sessionFactory并让它在Table1RepositoryTests中返回你想要的任何内容。
  3. 如果您确实想测试spring的注入逻辑,并使用sessionFactory测试Table1RepositoryImpl,则可以在代码中使用语法@RunWith(SpringJUnit4ClassRunner.class)。例外是因为no such method orphanRemovalGoogle您可以在stackoverflowblog
  4. 中找到答案