@DatabaseSetup 无法加载数据集

Posted

技术标签:

【中文标题】@DatabaseSetup 无法加载数据集【英文标题】:@DatabaseSetup unable to load data set 【发布时间】:2015-06-08 13:54:53 【问题描述】:

我通过 java config 配置了我的测试环境。在我的测试中,我需要一些预先的数据来运行它,但是当我使用 @DatabaseSetup 注释运行测试时,我总是得到错误

Unable to load dataset from "personTestData.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader

文件位于src/test/resources/personTestData.xml。出于好奇,我将文件移动到测试本身所在的同一个包中,仍然是同样的错误。

我花了无数个小时在网上搜索错误,直到现在没有解决方案。

更新

我使用的组件和版本:

junit: 4.12
hamcrest: 1.3
springtestdbunit: 1.2.1
dbunit: 2.5.0
bonecp: 0.8.0
hsqldb: 1.8.0.10

我的 xml

<?xml version="1.0" encoding="UTF-8"?>

<dataset>
    <person id="uuid'da578845-356c-4c99-9bda-c288c88e7b55" 
            dateCreated="2015-03-24 10:54:00"
            isEnabled="1"
            city="Graz"
            displayName="42"
            email="dent@example.com"
            firstname="Arthur"
            lastname="Dent"
            phone="01123456"
            street="Waßkannergasse 16"
            web="www.vdsoft.at"
            zip="1010"/>

    <person id="uuid'5aaae7dd-b84f-463e-a534-e7b2bc54752a" 
            dateCreated="2015-03-24 10:54:10"
            isEnabled="1"
            city="Graz"
            displayName="Towel"
            email="prefect@example.com"
            firstname="Ford"
            lastname="Prefect"
            phone="01123856"
            street="Waßkannergasse 45"
            web="www.vdsoft.at"
            zip="1010"/>

    <person id="uuid'b770b982-3a59-4292-9fd8-715afa2c1bef" 
            dateCreated="2015-03-24 10:54:20"
            isEnabled="1"
            city="Graz"
            displayName="Lemon"
            email="beeblebrox@example.com"
            firstname="Zaphod"
            lastname="Beeblebrox"
            phone="01125456"
            street="Waßkannergasse 20"
            web="www.vdsoft.at"
            zip="1010"/>
</dataset>

我的测试用例

    @Test
    @DatabaseSetup(type = DatabaseOperation.CLEAN_INSERT,
            value = "personTestData.xml")
    public void AddPersonAdditionsSuccessfull()         
        PersonAdditionsDbo data = new PersonAdditionsDbo();
        data.setCompany("VDsoft");
        data.setCompetences("Developer");
        data.setDistributor(true);
        data.setFunction("Teamleader");
        data.setHolisIsTopBecause("Payes rent...");
        data.setIamHolisBecauseNote("Work");
        data.setInvestment(10000f);
        data.setInvitedById(this.zaphodBeeblebrox.getId());
        data.setNotes("Something to say...");
        data.setPerson(this.athurDent);
        data.setReceiveNewsletter(true);
        data.setReceivePressMessages(true);
        data.setType("Developer");

        assertNull(data.getId());

        this.repo.save(data);

        assertNotNull(data.getId());
    

堆栈跟踪,为 db 的 init 截断

 WARN [main] (EntityManagerFactoryRegistry.java:80) - HHH000436: Entity manager factory name (TESTHolisEMF) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name'
 WARN [main] (TestContextManager.java:252) - Caught exception while allowing TestExecutionListener [com.github.springtestdbunit.DbUnitTestExecutionListener@23cd5d42] to process 'before' execution of test method [public void holis.backend.webapi.repository.PersonAdditionTest.AddPersonAdditionsSuccessfull()] for test instance [holis.backend.webapi.repository.PersonAdditionTest@2b44d6d0]
java.lang.IllegalArgumentException: Unable to load dataset from "personTestData.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at com.github.springtestdbunit.DbUnitRunner.loadDataset(DbUnitRunner.java:214)
    at com.github.springtestdbunit.DbUnitRunner.loadDataSets(DbUnitRunner.java:203)
    at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:184)
    at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:66)
    at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:186)
    at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:249)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:72)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:217)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
    at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:45)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
    at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
    at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

【问题讨论】:

你能用完整的堆栈跟踪和你使用@DatabaseSetup的测试方法更新你的问题吗? 【参考方案1】:

几天前我也遇到了同样的问题。

您的文件personTestData.xml 似乎在以下位置找不到(dataSet 为空):

IDataSet dataSet = dataSetLoader.loadDataSet(testContext.getTestClass(), dataSetLocation);

您是否尝试过使用 /personTestData.xml 代替?这解决了我的问题。

【讨论】:

这似乎也适用于我,至少我收到另一个错误。现在他无法插入 id,但是带有休眠和 mysql 的 UUID 可以在另一个页面上...谢谢 Julien 也适应我的情况,天哪,为什么要使用斜线

以上是关于@DatabaseSetup 无法加载数据集的主要内容,如果未能解决你的问题,请参考以下文章

无法手动加载 keras imdb 数据集

无法为 Pascal VOC 泡菜数据集加载泡菜

Spring Test DBUnit:无法从文件加载数据集

使用 RDLC 的报告无法加载数据集

无法验证装配数据;加载此程序集时必须提供授权密钥

无法将大型数据集加载到 h2 数据库中