I am trying to write an integration test within my Spring Boot application. I have problems getting the setup configuration right. The following Exception is thrown:
java.lang.IllegalArgumentException: Unable to load dataset from "sampleData.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader
Things I've already tried:
The sampleData.xml is in the same directory as my test class. I guess i am missing something stupid.
sampleData.xml:
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<Benutzer userId="1" firstName="peter" lastName="pan" email="peter@bla.de"/>
<Benutzer userId="2" firstName="hans" lastName="wurst" email="e@e.de"/>
</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("peter@bla.de"))
)
)))
.andExpect(model().attribute("Benutzer", hasItem(
allOf(
hasProperty("userid", is(2l)),
hasProperty("firstName", is("Hans")),
hasProperty("lastName", is("Wurst")),
hasProperty("email", is("e@e.de"))
)
)));
}
}
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;
}
}
When using Maven you have 2 directories under src/test
one for java
files and one for anything else resources
. If you put anything other as .java
files in src/test/java
those won't be processed and copied to the target/test-classes
directory and as such will not be available at the time the tests are run.
You need to put the .xml
files in the src/test/resources
directory to have them processed and copied.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.