@DatabaseSetup无法加载数据集

时间:2015-04-03 13:07:39

标签: java spring dbunit spring-test-dbunit

我通过java配置配置了我的测试环境。在我的测试中,我需要一些数据来预先运行它,但是当我使用@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的初始化

 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)

1 个答案:

答案 0 :(得分:16)

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

似乎找不到您的文件personTestData.xml(dataSet为空):

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

您是否尝试过放置/personTestData.xml?这解决了我的问题。