我试图通过从xml文件(dataset.xml)加载数据来运行dbunit测试用例,程序无法找到该表。我是dbunit的新手,缺乏良好的工作示例阻止我运行一个简单的程序。有人可以指导一下吗?
错误记录
SEVERE: Table 'states' not found in tableMap=org.dbunit.dataset.OrderedTableNameMap[_tableNames=[], _tableMap={}, _caseSensitiveTableNames=false]
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.64 sec <<< FAILURE!
testMe(com.istore.dao.DatabaseXmlTest) Time elapsed: 0.579 sec <<< ERROR!
org.dbunit.dataset.NoSuchTableException: states
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:156)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Results :
Tests in error:
testMe(com.istore.dao.DatabaseXmlTest): states
dataset.xml
<?xml version="1.0" encoding="UTF-8"?>
<dataset>
<states state_id="1" state_cd="NJ" name="New Jersey" tax_pct="6" active="Y"/>
<states state_id="2" state_cd="PA" name="Pennsylvania" tax_pct="6" active="Y"/>
</dataset>
DatabaseXmlTest.java
public class DatabaseXmlTest extends DataSourceBasedDBTestCase {
public static final String url = "jdbc:hsqldb:mem:my-project-test;shutdown=true";
public static final String driver = "org.hsqldb.jdbcDriver";
public static final String user = "";
public static final String pwd = "";
@Override
protected DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUsername(user);
dataSource.setPassword(pwd);
dataSource.setUrl(url);
return dataSource;
}
@Override
protected IDataSet getDataSet() throws Exception {
InputStream inputStream = getClass().getResourceAsStream("/dataset.xml");
Reader reader = new InputStreamReader(inputStream);
FlatXmlDataSet dataset = new FlatXmlDataSet(reader);
return dataset;
}
public void testMe() throws Exception {
DataSource dataSource = getDataSource();
HsqldbConnection cn = new HsqldbConnection(dataSource.getConnection(), null);
DatabaseOperation.CLEAN_INSERT.execute(cn, getDataSet());
//get all states
List<State> states = new StateDao(dataSource).getAllStates();
assertNotNull(states);
}
}
StateDao.java
public class StateDao {
private static final Logger LOG = Logger.getLogger(StateDao.class);
protected DataSource dataSource;
public StateDao(DataSource dataSource) {
this.dataSource = dataSource;
}
public List<State> getAllStates() throws SQLException {
String sql = "select * from states a where a.active='Y'";
StateQueryExecutor executor = new StateQueryExecutor(dataSource.getConnection(), sql);
List<State> states = executor.getAllStates();
LOG.debug("states = " + states);
return states;
}
}