我寫了一個 Spring Boot 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 - 在 XML 檔案中添加“”
- 在 XML 檔案中添加版本
- 洗掉
jupiter和使用junit.Test - 檢查 XML 的位置
- 添加類路徑在
我猜原因是找不到XML檔案(因為我隨意更改檔案名它仍然有這個錯誤),也許是我的Spring Boot版本和JUnit版本不支持DbUnit?
uj5u.com熱心網友回復:
嘗試這樣的事情
@DatabaseSetup("/data/.../studentTestSample.xml")
小心您的檔案路徑將幫助您解決問題。
參考檔案https://springtestdbunit.github.io/spring-test-dbunit/apidocs/com/github/springtestdbunit/annotation/DatabaseSetup.html部分引數value。
uj5u.com熱心網友回復:
我終于找到了問題所在。那是因為xml的格式不對!
這是我的 xml 名稱:
studentTestSample
應該這樣寫:
studentTestSample.xml
我記得我在創建這個檔案時寫了“studentTestSample.xml”。看起來我必須強調我的xml檔案的型別。
最后,確保在鍵入 command B 時可以跳轉到檔案所在的位置。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/362786.html
