我正在尝试使用dbunit将XML文件加载到mySql,我的FlatXmlDataSet显示

“注意:此元素既没有附加源也没有附加Javadoc,因此找不到Javadoc”

但是得到AsserionFailedError:null

dbunit版本-> 2.4.9
mySql-> 5.2

public class DbUnitSampleTest extends TestCase {

public static final String TABLE_LOGIN ="login";
private FlatXmlDataSet loadedDataSet;

protected IDatabaseConnection getConnection() throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConnection =DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");

return new DatabaseConnection(jdbcConnection);
}

@SuppressWarnings("deprecation")
protected IDataSet getDataSet() throws Exception
{
        loadedDataSet = new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream("input.xml"));

        return loadedDataSet;
}

public void testCheckLoginDataLoaded() throws Exception
{
assertNotNull(loadedDataSet);
int rowCount = loadedDataSet.getTable(TABLE_LOGIN).getRowCount();
TestCase.assertEquals(2, rowCount);
}
}


错误:

junit.framework.AssertionFailedError: null
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertNotNull(Assert.java:214)
at junit.framework.Assert.assertNotNull(Assert.java:207)
at DbUnitSampleTest.testCheckLoginDataLoaded(DbUnitSampleTest.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

最佳答案

问题在这里

loadedDataSet = new FlatXmlDataSet(this.getClass().getClassLoader().getResourceAsStream("input.xml"));


DBUnit的错误消息不正确。问题在于资源加载器找不到您的input.xml文件。尝试像这样将其分离出来。

InputStream is = this.getClass().getClassLoader().getResourceAsStream("input.xml")
if(is != null)
   loadedDataSet = new FlatXmlDataSet(is);
else
   System.out.println("Can't find input.xml :(");


从那里您应该能够找到为什么找不到您的input.xml。

关于java - Java无法使用FlatXmlDataSet读取XML文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15739114/

10-13 00:05