首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用类com.github.springtestdbunit.dataset.FlatXmlDataSetLoader从"<filePath>“加载数据集

无法使用类com.github.springtestdbunit.dataset.FlatXmlDataSetLoader从"<filePath>“加载数据集
EN

Stack Overflow用户
提问于 2021-11-22 03:24:12
回答 2查看 408关注 0票数 0

我编写了Spring + JPA的演示程序,在这里我遇到了一个问题,当我尝试在存储库上做一些测试时,我几天都很困惑。错误是

代码语言:javascript
运行
复制
**java.lang.IllegalArgumentException: Unable to load dataset from"dbunit/studentTestSample" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoaderava.lang.IllegalArgumentException: Unable to load dataset from "/data/dbunit/studentTestSample.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader
at org.springframework.util.Assert.notNull(Assert.java:201) ~[spring-core-5.3.12.jar:5.3.12]
    at com.github.springtestdbunit.DbUnitRunner.loadDataset(DbUnitRunner.java:211) ~[spring-test-dbunit-1.3.0.jar:na]
    at com.github.springtestdbunit.DbUnitRunner.loadDataSets(DbUnitRunner.java:192) ~[spring-test-dbunit-1.3.0.jar:na]
    at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:173) ~[spring-test-dbunit-1.3.0.jar:na]
    at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:75) ~[spring-test-dbunit-1.3.0.jar:na]
    at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:185) ~[spring-test-dbunit-1.3.0.jar:na]**

首先,这是我的测试文件

代码语言:javascript
运行
复制
    package com.meds.infrastructure.student;
    
    
import com.github.springtestdbunit.TransactionDbUnitTestExecutionListener;
    import com.github.springtestdbunit.annotation.DatabaseSetup;
    import com.meds.domain.student.entity.StudentInfoDo;
    import com.meds.infrastructure.repository.StudentRepository;
    import static org.assertj.core.api.Assertions.assertThat;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.TestExecutionListeners;
    import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
    import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
    import org.springframework.transaction.annotation.Transactional;
    
    @AutoConfigureMockMvc
    @SpringBootTest
    @ContextConfiguration
    @TestExecutionListeners({
            DependencyInjectionTestExecutionListener.class,
            DirtiesContextTestExecutionListener.class,
            TransactionDbUnitTestExecutionListener.class,
            MockitoTestExecutionListener.class
    })
    public class StudentRepositoryTest {
    
        @Autowired
        private StudentRepository studentRepository;
    
        @Test
        @Transactional
        @DatabaseSetup("dbunit/studentTestSample")
        public void should_get_student_info_id() {
    
            StudentInfoDo studentInfoDo = studentRepository.findStudentById(1l);
    
            assertThat(studentInfoDo.getId()).isEqualTo(1l);
            assertThat(studentInfoDo.getStudentId()).isEqualTo("studentId");
            assertThat(studentInfoDo.getName()).isEqualTo("studentName1");
            assertThat(studentInfoDo.getGender()).isEqualTo("MALE");
        }
    }

这是我的gradle.build

代码语言:javascript
运行
复制
    dependencies {
        implementation 'junit:junit:4.13.1'
        annotationProcessor 'org.mapstruct:mapstruct-processor:1.2.0.Final'
        annotationProcessor 'org.projectlombok:lombok:1.18.10'
        compileOnly 'org.projectlombok:lombok:1.18.10'
        compileOnly 'org.mapstruct:mapstruct-processor:1.2.0.Final'
    
        implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation 'org.springframework.boot:spring-boot-starter-web'
        implementation 'org.hibernate:hibernate-core'
        implementation 'javax.xml.bind:jaxb-api:2.3.0'
        implementation 'javax.xml.soap:saaj-api:1.3.5'
        implementation 'jakarta.validation:jakarta.validation-api:2.0.2'
        implementation 'org.flywaydb:flyway-core'
        implementation 'org.mapstruct:mapstruct-jdk8:1.2.0.Final'
        implementation 'org.mapstruct:mapstruct:1.3.1.Final'
        implementation 'io.springfox:springfox-swagger2:2.8.0'
        implementation 'io.springfox:springfox-swagger-ui:2.8.0'
        implementation 'org.springframework.boot:spring-boot-starter-security'
    
        runtimeOnly 'mysql:mysql-connector-java'
    
        testImplementation("org.dbunit:dbunit:2.5.2")
        testImplementation("com.github.springtestdbunit:spring-test-dbunit:1.3.0")
        testImplementation 'org.springframework.boot:spring-boot-starter-test'
    }

这是我的studentTestSample.xml

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <student_info id="1" student_id="studentId1" student_name="studentName1" gender="MALE" grouped="0" />
</dataset>

我尝试过的@DatabaseSetup

  • add
    1. 添加类路径在XML文件中“at XML file
    2. 添加版本at XML文件
    3. 删除jupiter并使用XML

的位置”

我想原因是找不到XML文件(因为我随机更改文件名,它仍然有这个错误),也许是关于我的Spring版本和JUnit版本不支持DbUnit?

EN

回答 2

Stack Overflow用户

发布于 2021-11-22 03:42:06

试试像这样的东西

代码语言:javascript
运行
复制
@DatabaseSetup("/data/.../studentTestSample.xml")

小心您的文件路径将帮助您解决问题。

参考文档https://springtestdbunit.github.io/spring-test-dbunit/apidocs/com/github/springtestdbunit/annotation/DatabaseSetup.html节参数value

票数 0
EN

Stack Overflow用户

发布于 2021-11-22 08:33:29

我终于发现了这个问题。这是因为xml格式不正确!

以下是我的xml名称:

studentTestSample

它的写法应该是:

studentTestSample.xml

我确实记得我在创建这个文件时写了"studentTestSample.xml“。看起来,我必须强调xml文件的类型。

最后,在输入command+B时,确保可以跳转到文件的位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70060569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档