【发布时间】:2020-11-09 01:02:21
【问题描述】:
我正在使用带有 h2 内存数据库的 dbunit 来测试我编写的 DAO 类中的方法。套件中的所有测试过去都成功通过,但后来我重新组织了项目的目录结构,现在运行测试套件时出现 NoSuchTableException。我觉得这一定是某种构建路径错误,但我已经为此头疼了两天,无法修复它。
以下是包含一些设置方法的测试类的摘录:
@RunWith(PowerMockRunner.class)
@PrepareForTest(DAOUtilities.class)
public class FeedingScheduleDaoImplDBUnitTest extends DataSourceBasedDBTestCase {
private Connection connection;
private FeedingScheduleDaoImpl fsdi = new FeedingScheduleDaoImpl();
@Override
protected DataSource getDataSource() {
JdbcDataSource dataSource = new JdbcDataSource();
dataSource.setUrl("jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'");
dataSource.setUser("sa");
dataSource.setPassword("sa");
return dataSource;
}
@Override
protected IDataSet getDataSet() throws Exception {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("data.xml");
return new FlatXmlDataSetBuilder().build(inputStream);
}
@Override
protected DatabaseOperation getSetUpOperation() {
return DatabaseOperation.REFRESH;
}
@Override
protected DatabaseOperation getTearDownOperation() {
return DatabaseOperation.DELETE_ALL;
}
@Before
public void setUp() throws Exception {
super.setUp();
connection = getConnection().getConnection();
}
@After
public void tearDown() throws Exception {
super.tearDown();
}
@Test
public void givenDataSetEmptySchema_whenDataSetCreated_thenTablesAreEqual() throws Exception {
IDataSet expectedDataSet = getDataSet();
ITable expectedTable = expectedDataSet.getTable("animals");
IDataSet databaseDataSet = getConnection().createDataSet();
ITable actualTable = databaseDataSet.getTable("animals");
Assertion.assertEquals(expectedTable, actualTable);
}
}
实际上是 return new FlatXmlDataSetBuilder().build(inputStream); 产生了异常,但我怀疑这是因为架构没有在 getDataSource() 方法中正确加载。
以下是我重组后的新目录结构。测试所需的sql schema文件和xml数据文件都在src/test/resources目录下。
这是我的 pom.xml 的构建部分。我没有为构建指定任何源或资源目录,所以如果我理解正确,那应该将所有资源文件放在默认的 target/test-classes 目录中。我查过了,它们确实在那里。
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
</plugin>
</plugins>
</build>
任何提示或帮助将不胜感激。我仍在学习如何使用 Maven,并且我进行了这次重组以更好地理解标准的 Maven 项目结构,所以我希望既能修复我的问题,又能学习如何更好地使用 Maven。
【问题讨论】:
标签: java maven jdbc classpath dbunit