简体   繁体   English

测试中未加载 Spring Boot *.hbm.xml 映射文件

[英]Spring Boot *.hbm.xml mapping files not loaded in test

The problem is hard to explain, so please have a look at the project: https://github.com/darzz/boot_bug This is the minimal setup, that reproduces the bug.这个问题很难解释,所以请看一下项目: https : //github.com/darzz/boot_bug这是最小的设置,重现了错误。

Description : The application stack is Spring Boot with Spring Data and Spring Batch.描述:应用程序堆栈是带有 Spring Data 和 Spring Batch 的 Spring Boot。 There is testNamedQuery.hbm.xml file under src/main/resources/queries . src/main/resources/queries下有testNamedQuery.hbm.xml文件。

When running from Application class the batch job finishes successfully, no exceptions in logs.应用程序类运行时,批处理作业成功完成,日志中没有异常。 However, when running from ApplicationNotWorking class, which is exact copy, just put in test source root, the batch job fails:但是,当从ApplicationNotWorking类运行时,这是精确的副本,只需放入测试源根目录,批处理作业就会失败:

Caused by: org.hibernate.MappingException: Named query not known: findPersonNames
    at org.hibernate.internal.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:177) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
    at org.springframework.batch.item.database.HibernateItemReaderHelper.createQuery(HibernateItemReaderHelper.java:146) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.batch.item.database.HibernateItemReaderHelper.getForwardOnlyCursor(HibernateItemReaderHelper.java:123) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.batch.item.database.HibernateCursorItemReader.doOpen(HibernateCursorItemReader.java:185) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]
    ... 39 common frames omitted

So it looks like that when running tests, *.hbm.xml files are not loaded!所以看起来在运行测试时,*.hbm.xml 文件没有加载! After research and debugging, I think, I might have found the cause - persistence unit root url is set for tests to target/test-classes, but the mapping files are in /target/classes.经过研究和调试,我想,我可能已经找到了原因——持久化单元根 url 被设置为测试目标/测试类,但映射文件在 /target/classes 中。

Possible cause in my opinion may be similar to what's described in here http://blog.carbonfive.com/2007/05/17/using-classpath-vs-classpath-when-loading-spring-resources/我认为可能的原因可能与此处描述的类似http://blog.carbonfive.com/2007/05/17/using-classpath-vs-classpath-when-loading-spring-resources/

But I have no idea, how to solve this issue in Spring Boot, without creating persistence.xml configuration just for testing purposes.但是我不知道如何在 Spring Boot 中解决这个问题,而不是为了测试目的而创建 persistence.xml 配置。 Don't want to copy *.hbm.xml files from main/resources to test/resources either.也不想将 *.hbm.xml 文件从 main/resources 复制到 test/resources。

Does anyone have an idea?有没有人有想法?

@Autowired
private ResourceLoader rl;


@Bean
public LocalSessionFactoryBean sessionFactory() throws IOException {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setMappingLocations(loadResources());
    return sessionFactoryBean;
}

public Resource[] loadResources() {
    Resource[] resources = null;
    try {
        resources = ResourcePatternUtils.getResourcePatternResolver(rl)
            .getResources("classpath:/hibernate/*.hbm.xml");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return resources;
}

If I understood the problem correctly, You want to run Spring Boot Test which should pick up *.hbm files from classpath.如果我正确理解了问题,您想运行 Spring Boot Test,它应该从类路径中获取 *.hbm 文件。

Where did you keep your *.hbm files?你把 *.hbm 文件放在哪里?

You have to ensure that all *.hbm files are kept under src/main/resources.您必须确保所有 *.hbm 文件都保存在 src/main/resources 下。 Hence when you run test class which will call the actual class which refers to *.hbm files from src/main/resources.因此,当您运行测试类时,它将调用引用来自 src/main/resources 的 *.hbm 文件的实际类。

If above solution does not help then you need to share your project file structure.如果上述解决方案没有帮助,那么您需要共享您的项目文件结构。

I looked into the git project.我查看了 git 项目。 It seems you need to change the ApplicationNotWorking.java since it is a test class hence should be something as below:似乎您需要更改 ApplicationNotWorking.java 因为它是一个测试类,因此应该如下所示:

//src/test/java //源代码/测试/Java

package bug;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.stackoverflow.springboot.BatchProcessing;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class )
public class ApplicationNotWorking {
    //Inject required bean which you want to test.
    @Autowired
    private BatchProcessing bProcess;
    //If above @Autowired BatchProcessing  do not work then you can object  
    //directly to kick off the test. BatchProcessing bProcess = new 
    //BatchProcessing();    

    //This way you can test each method
    @Test
    public void testBatchProcessing(){
        System.out.println("***BatchProcessing: " + bProcess.batchProcess());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM