我编写了Spring + JPA的演示程序,在这里我遇到了一个问题,当我尝试在存储库上做一些测试时,我几天都很困惑。错误是
**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]**
首先,这是我的测试文件
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
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
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<student_info id="1" student_id="studentId1" student_name="studentName1" gender="MALE" grouped="0" />
</dataset>
我尝试过的@DatabaseSetup
jupiter
并使用XML的位置”
我想原因是找不到XML文件(因为我随机更改文件名,它仍然有这个错误),也许是关于我的Spring版本和JUnit版本不支持DbUnit?
发布于 2021-11-22 03:42:06
试试像这样的东西
@DatabaseSetup("/data/.../studentTestSample.xml")
小心您的文件路径将帮助您解决问题。
发布于 2021-11-22 08:33:29
我终于发现了这个问题。这是因为xml格式不正确!
以下是我的xml名称:
studentTestSample
它的写法应该是:
studentTestSample.xml
我确实记得我在创建这个文件时写了"studentTestSample.xml“。看起来,我必须强调xml文件的类型。
最后,在输入command+B时,确保可以跳转到文件的位置。
https://stackoverflow.com/questions/70060569
复制相似问题