0
我正在尝试在我的Spring Boot应用程序中编写集成测试。我在安装配置时遇到问题。以下异常被抛出:春季测试+ DBUnit无法加载数据集
java.lang.IllegalArgumentException: Unable to load dataset from "sampleData.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader
事情我已经尝试:
- 更改sampleData.xml文件的名称。
- 使用value =“”符号。
- 使用classpath:在文件名之前。
- 在文件名之前使用附加的“/”。
sampleData.xml与我的测试类位于同一个目录中。我想我错过了一些愚蠢的东西。
sampleData.xml:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<Benutzer userId="1" firstName="peter" lastName="pan" email="[email protected]"/>
<Benutzer userId="2" firstName="hans" lastName="wurst" email="[email protected]"/>
</dataset>
BenutzerVerwaltungsIntegrationsTest:
RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = WebDelegatingSmartContextLoader.class, classes = {ExampleApplicationContext.class})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class})
@DatabaseSetup("sampleData.xml")
@WebAppConfiguration
public class BenutzerverwaltungIntegrationTest {
@Resource
private WebApplicationContext webApplicationContext;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
@ExpectedDatabase("sampleData.xml")
public void findAll() throws Exception {
mockMvc.perform(get("/verwaltung"))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/jsp/verwaltung.jsp"))
.andExpect(model().attribute("Benutzer", hasSize(2)))
.andExpect(model().attribute("Benutzer", hasItem(
allOf(
hasProperty("userId", is(1l)),
hasProperty("firstName", is("peter")),
hasProperty("lastName", is("pan")),
hasProperty("email", is("[email protected]"))
)
)))
.andExpect(model().attribute("Benutzer", hasItem(
allOf(
hasProperty("userid", is(2l)),
hasProperty("firstName", is("Hans")),
hasProperty("lastName", is("Wurst")),
hasProperty("email", is("[email protected]"))
)
)));
}
}
ExampleApplicationContext:
@Configuration
@PropertySource("classpath:application.properties")
public class ExampleApplicationContext {
@Resource
private Environment environment;
@Bean
public DataSource dataSource(){
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setURL(environment.getProperty("spring.datasource.url"));
dataSource.setUser(environment.getProperty("spring.datasource.username"));
dataSource.setPassword(environment.getProperty("spring.datasource.password"));
return dataSource;
}
}
xml文件应该放在'src/test/resources'而不是'src/test/java'中,将它移动到正确的位置。 –
试过了。没有改变。这可能是我的pom.xml的问题吗? – user1651804
不,但你的xml应该在资源路径而不是java路径。如果你在后者有它,它不会被复制到目标文件夹。在将它移动到资源时,您是否保留了目录结构或将其直接移到了资源中? –