1

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:

  1. Changing the name of the sampleData.xml file.
  2. Using value="" notation.
  3. Using classpath: before the file name.
  4. Using an additional "/" before the file name.

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;
    }
}
user1651804
  • 55
  • 2
  • 8
  • 1
    xml files should go in `src/test/resources` not `src/test/java`, move it to the correct location. – M. Deinum Dec 11 '15 at 07:02
  • Tried it. Nothing changed. Could it be a problem with my pom.xml? – user1651804 Dec 11 '15 at 07:20
  • 1
    No. But your xml should be in the resources path not the java path. If you have it in the latter it will not be copied to the target folder. When moving it to the resources did you keep the directory structure or moved it to the resources directly? – M. Deinum Dec 11 '15 at 07:24
  • I recreated the package structure from src/test/java but wrongfully. I created one Package which were actually two packages. After I created them both individually it worked. Thanks for your time! If you post an answer I can accept it. – user1651804 Dec 11 '15 at 07:50
  • 1
    You may also consider using [DbSetup](http://dbsetup.ninja-squad.com/) instead of `DbUnit`. Less trouble with data sets' re-usage and also lack of pain with management of the huge amount of small XML files. – G. Demecki Dec 11 '15 at 08:12

1 Answers1

1

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.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224