DBUnit JUnit Entegrasyonu

DBUnit JUnit ile entegre edilerek entegrasyon ve regresyon testlerinin JUnit testleri olarak implemente edilmesi mümkündür. DBUnit JUnit entegrasyonu aşağıdaki şekilde gerçekleştirilebilir. Bilgibankası olarak örnekte HSQLDB kullanılmıştır (bakınız HSQLDB JUnit entegrasyonu). Verilerin dbunit-dataset.xml dosyasında tanımlanmıs olması gerekmektedir. setUp() metodu her test öncesi JUnit frameworkü tarafından koşturulacağı için istenilen veriler test öncesi bilgibankasına eklenmiş ve regresyon testleri için taban oluşturulmuş olacaktır.

	
// JDBC Driver
    public static final String DRIVERCLASS      = "org.hsqldb.jdbcDriver";    
    // standalone mode
    public static final String CONNECTIONURL    = "jdbc:hsqldb:hsql://localhost:9006/mydb";
    public static final String USERNAME         = "sa";
    public static final String PASSWORD         = "";

public void setUp() throws Exception
	{
		PreparedStatement pstmt = null;
		
		 try
		 {
			IDatabaseConnection connection = getConnection();
			IDataSet dataSet = getDataSet();
			try
			{
				// REF INTEGRITY MUST BE DISABLED IN ORDER TO IMPORT THE DATA
				// THIS SOLUTION IS ONLY VALID FOR HSQLDB 1.8
				pstmt = connection.getConnection().prepareStatement("set referential_integrity FALSE");
				pstmt.executeUpdate();
				DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
				pstmt = connection.getConnection().prepareStatement("set referential_integrity TRUE");
				pstmt.executeUpdate();
			}
			finally
			{
				connection.close();
			}
		}
		catch (Exception e)
		{
		    throw e;
		}
	}



	protected IDatabaseConnection getConnection() throws Exception
	{
		Class.forName(DRIVERCLASS);
		Connection jdbcConnection = DriverManager.getConnection(CONNECTIONURL, USERNAME, PASSWORD);
		IDatabaseConnection con =  new DatabaseConnection(jdbcConnection);
		DatabaseConfig config = con.getConfig();
		config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new HsqldbDataTypeFactory());
		return con;
	}




protected final IDataSet getDataSet() throws Exception
	{
		final URL url = DatabaseTestCase.class.getResource( "/dbunit-dataset.xml");
	    final File file = new File(url.getPath());
	    return new FlatXmlDataSet(file);
	}


Püf Noktası kategorisinden son yazılar

Share Button
0.00 avg. rating (0% score) - 0 votes

Bir cevap yazın