Spring test + DBUnit无法加载DataSet

时间:2015-12-11 06:54:06

标签: java spring spring-mvc dbunit spring-test-dbunit

我正在尝试在Spring Boot应用程序中编写集成测试。我在设置配置方面遇到问题。抛出以下异常:

java.lang.IllegalArgumentException: Unable to load dataset from "sampleData.xml" using class com.github.springtestdbunit.dataset.FlatXmlDataSetLoader

我已经尝试过的事情:

  1. 更改sampleData.xml文件的名称。
  2. 使用value =“”表示法。
  3. 使用classpath:在文件名之前。
  4. 在文件名前添加“/”。
  5. sampleData.xml与我的测试类位于同一目录中。我想我错过了一些愚蠢的东西。

    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;
        }
    }
    

1 个答案:

答案 0 :(得分:1)

使用Maven时,src/test下有2个java文件目录,resources文件中有2个目录.java。如果您在src/test/java中放置了target/test-classes个其他文件,那么这些文件将被处理并复制到.xml目录中,因此在运行测试时将无法使用

您需要将src/test/resources文件放在$row目录中,以便对其进行处理和复制。