How to get the path of src/test/resources directory in JUnit?

前端 未结 14 1793
时光取名叫无心
时光取名叫无心 2020-11-28 01:25

I know I can load a file from src/test/resources with:

getClass().getResource(\"somefile\").getFile()

But how can I get the full path to th

14条回答
  •  离开以前
    2020-11-28 02:16

    Use the following to inject Hibernate with Spring in your unit tests:

    @Bean
    public LocalSessionFactoryBean getLocalSessionFactoryBean() {
        LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
        localSessionFactoryBean.setConfigLocation(new ClassPathResource("hibernate.cfg.xml"));
        localSessionFactoryBean.setPackagesToScan("com.example.yourpackage.model");
        return localSessionFactoryBean;
    }
    

    If you don't have the hibernate.cfg.xml present in your src/test/resources folder it will automatically fall back to the one in your src/main/resources folder.

提交回复
热议问题