Home > Mobile >  Unable to load dataset from "<filePath>" using class com.github.springtestdbunit.dat
Unable to load dataset from "<filePath>" using class com.github.springtestdbunit.dat

Time:11-22

I wrote a demo of Spring Boot JPA, and here I met a question that confused me couple of days when I try to do some tests on repository. The error is

**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]**
  1. Firstly, here is my test file
    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");
        }
    }

and here is my gradle.build

    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'
    }

Here is my studentTestSample.xml

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
    <student_info id="1" student_id="studentId1" student_name="studentName1" gender="MALE" grouped="0" />
</dataset>
  1. What I have tried
    1. add classpath at @DatabaseSetup
    2. add "" at XML file
    3. add version at XML file
    4. remove jupiter and use junit.Test
    5. Check the location of XML

I guess the reason is cannot find the XML file(because I change the file name randomly it still has this error), maybe it's about my Spring Boot version and JUnit version does not support DbUnit?

CodePudding user response:

Try something like this

@DatabaseSetup("/data/.../studentTestSample.xml")

Careful with your file path will help you resolve problem.

Reference document https://springtestdbunit.github.io/spring-test-dbunit/apidocs/com/github/springtestdbunit/annotation/DatabaseSetup.html section parameter value.

CodePudding user response:

I finally found the issue, it because the format of xml is not right!

here is my xml name: studentTestSample

it should be written like is: studentTestSample.xml

I do remember I wrote "studentTestSample.xml" when I created this file. it looks like I have to emphasize the type of my xml file.

finally....make sure you can jump to the location of file when you typing command B

  • Related