2013-04-01 33 views
0

我試圖用DbUnit的加載XML文件到MySQL的XML文件,我FlatXmlDataSet表明的java無法讀取使用FlatXmlDataSet

「注:該元素既不具有附加源也沒有附着的Javadoc,因此沒有的Javadoc可能找到」

但要AsserionFailedError:空

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) 

回答

1

的問題是在這裏

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。