[英]Spring Boot *.hbm.xml mapping files not loaded in test
這個問題很難解釋,所以請看一下項目: https : //github.com/darzz/boot_bug這是最小的設置,重現了錯誤。
描述:應用程序堆棧是帶有 Spring Data 和 Spring Batch 的 Spring Boot。 src/main/resources/queries下有testNamedQuery.hbm.xml文件。
從應用程序類運行時,批處理作業成功完成,日志中沒有異常。 但是,當從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
所以看起來在運行測試時,*.hbm.xml 文件沒有加載! 經過研究和調試,我想,我可能已經找到了原因——持久化單元根 url 被設置為測試目標/測試類,但映射文件在 /target/classes 中。
我認為可能的原因可能與此處描述的類似http://blog.carbonfive.com/2007/05/17/using-classpath-vs-classpath-when-loading-spring-resources/
但是我不知道如何在 Spring Boot 中解決這個問題,而不是為了測試目的而創建 persistence.xml 配置。 也不想將 *.hbm.xml 文件從 main/resources 復制到 test/resources。
有沒有人有想法?
@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;
}
如果我正確理解了問題,您想運行 Spring Boot Test,它應該從類路徑中獲取 *.hbm 文件。
你把 *.hbm 文件放在哪里?
您必須確保所有 *.hbm 文件都保存在 src/main/resources 下。 因此,當您運行測試類時,它將調用引用來自 src/main/resources 的 *.hbm 文件的實際類。
如果上述解決方案沒有幫助,那么您需要共享您的項目文件結構。
我查看了 git 項目。 似乎您需要更改 ApplicationNotWorking.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.