日期:2014-05-16  浏览次数:20515 次

JUNIT结合DBUNIT进行单元测试初试

????? 涉及数据库操作的单元测试,最麻烦的就是基础数据准备、正确的验证数据。这两项工作以前我都是手工完成,存储过程、SQL搞得挺乱,最近试了试DBUNIT还是不错,为准备数据提供了一种思路。

?

测试类的基类如下:

public class TestBase extends DBTestCase
{
	private static String[] paths ={"conf/spring-context.xml"};
	private static ApplicationContext ctx = null;
	
	private static final String DEFAULT_LOG4J_XML_CONFIG_FILE =
		"conf/log4j.properties";
	protected Logger log = Logger.getLogger(getClass());
	private String name;
	private static IDataSet databaseDataSet;
	
	static {
    	     PropertyConfigurator.configure(DEFAULT_LOG4J_XML_CONFIG_FILE);
    	     ctx = new FileSystemXmlApplicationContext(paths);
	}
	
	public TestBase(){}

	public void init(boolean isInit){
	     System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:oracle:thin:@localhost:1521:orcl" );
	     System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "oracle.jdbc.driver.OracleDriver" );
	     System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME, "SSS" );
	     System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD, "SSS" );
	     System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, "APP" );
	}
	
	
	public Object getBean(String beanName){
		return ctx.getBean(beanName);
	}
	

                /**
	 * 初始化数据库连接
	 * @throws SQLException
	 * @throws Exception
	 */
	protected  void initConn(){
		try {
			databaseDataSet = getConnection().createDataSet();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 业务方法执行后,取数据库中的记录
	 * @param tableName
	 * @return
	 * @throws DataSetException
	 */
	protected static ITable getDbTable(String tableName) throws DataSetException{
		return databaseDataSet.getTable(tableName);
	}
	
	/**
	 * 从XML文件中取预先准备的正确数据,用于验证db数据。
	 * @param tableName
	 * @return
	 * @throws DataSetException
	 * @throws Exception
	 */
	protected ITable getXmlTable(String tableName) throws DataSetException, Exception{
		return  new XmlDataSet(new FileInputStream("validatedata/"+tableName+".xml")).getTable(tableName);
	}
	
    protected DatabaseOperation getSetUpOperation() throws Exception
    {
        return DatabaseOperation.NONE;
    }

    protected DatabaseOperation getTearDownOperation() throws Exception
    {
        return DatabaseOperation.DELETE_ALL;
    }

?具体一个测试类:

public class LogFtpletTest extends TestBase {

 LogFtplet log = new LogFtplet();


 public void setUp() {
       super.initConn();
 }
 
 public void testOnFend() throws DataSetException,
   DatabaseUnitException, Exception {


  log.onAppendStart(session, request);//业务方法


  ITable expectedTable = getXmlTable("T_RECEIVE_STATUS");//事先准备的XML正确数据

  ITable actualTable = getDbTable("T_RECEIVE_STATUS");//业务方法执行完后,数据库表数据
  Assertion.assertEqualsIgnoreCols(expectedTable, 
    actualTable, 
    new String[] {"S_ID",  "START_TIME", 
       "UPDATE_TIME"}); //比较需要的列值


  assertEquals(new BigDecimal(2), actualTable.getValue(0, "STATUS"));//比较需要的列值
 }

}

?