1

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. ファイル名の前に追加の「/」を使用します。

sampleData.xml は、テスト クラスと同じディレクトリにあります。私は愚かな何かを見逃していると思います。

サンプルデータ.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;
    }
}
4

1 に答える 1

1

Maven を使用する場合、ファイル用とその他src/test用の2 つのディレクトリがあります。ファイルとして他のものを入れた場合、それらは処理されず、ディレクトリにコピーされないため、テストの実行時に使用できなくなります。javaresources.javasrc/test/javatarget/test-classes

ファイルを処理してコピーするには、.xmlファイルをディレクトリに配置する必要があります。src/test/resources

于 2015-12-11T08:00:29.027 に答える