DBunit - DbAssertionFailedError

时间:2015-03-25 13:26:46

标签: java dbunit

对DBUnit感到沮丧:(任何人都知道为什么我会得到dbAssertionFailedError driverClass对于下面的请求是否为空?dbunitData.xml包含一行的测试数据。我知道与数据库的连接很好但是错误似乎是由断言引发。

public class ExtendDBTestCaseTest extends DBTestCase
{
    public static final String TABLE_LOGIN = "salarydetails";
    private FlatXmlDataSet loadedDataSet;
    private SalaryCalculation salaryCalculation;

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

        return loadedDataSet;
    }

    protected void setUp() throws Exception 
    {
        setUpDatabase();
    }

    @SuppressWarnings("deprecation")
    private void setUpDatabase() throws Exception 
    {   
        Class.forName("com.mysql.jdbc.Driver");
        Connection jdbcConnection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/salary", "someUser", "somePass");

        IDatabaseConnection connection = new DatabaseConnection(jdbcConnection);
        IDataSet dataSet = getDataSet();                

        try 
        {
            getSetUpOperation().execute(connection, dataSet);
        } 
        finally 
        {
            connection.close();
        }               
    }

    protected DatabaseOperation getSetUpOperation() throws Exception{
        return DatabaseOperation.REFRESH;
    }

    public void testCalculatorNeg() throws Exception 
    {
        salaryCalculation = new SalaryCalculation();
        int salary = salaryCalculation.calculator("12345");
        assertEquals(0, salary);
    }
}

1 个答案:

答案 0 :(得分:1)

令人沮丧的是,经过更多的反复试验,我回答了自己的问题。只需要在loadedDataSet.endDataSet()中添加一个拆解方法;现在工作正常!