2011-09-01 129 views
5

我在我的代碼庫中的一個類中有一個方法,對於我來說,我無法參與我的junit測試。 基本上當我請求一個數據庫連接這個類被調用時,如果返回一個陳舊的連接,新的連接建立Java - 代碼覆蓋

這是在我的課(下調爲這個目的)

public class TCSOracleDataSourceWrapper extends OracleDataSource { 

private static final int STALE_CONNECTION_EX_CODE = 17143; 
private OracleConnectionCacheManager cacheManager; 
private String cacheName; 
/** Local log variable **/ 
private final Log logger = LogFactory.getLog(getClass()); 


/** 
* Class constructor 
* @throws SQLException 
*/ 
public TCSOracleDataSourceWrapper() throws SQLException { 
    super(); 
} 

private static final long serialVersionUID = 1L; 

@Override 
/** 
* Get a connection but if the connection is stale then refresh all DB connections 
* 
*/ 
public final Connection getConnection() throws SQLException { 

    logger.debug("Retrieving a database connection from the pool"); 

    Connection connection = null; 
    try{ 
     connection = super.getConnection();   
    } 
    catch(SQLException e) 
    { 

     if(e.getErrorCode() == STALE_CONNECTION_EX_CODE) 
     {    
      logger.error("Stale Oracle connection found in the Connection Pool. Refreshing invalid DB connections."); 
      //refresh invalid connections 
      cacheManager.refreshCache(cacheName, OracleConnectionCacheManager.REFRESH_INVALID_CONNECTIONS); 
      //now try to get the connection again 
      connection = super.getConnection(); 
     } 
     else 
     { 
      throw e; 
     } 
    }  

    return connection; 
}} 
的mthod的片段

任何想法如何確保我的junit測試執行if語句? 我目前使用的EasyMock和Powermock但如果使用statment這些工具

所有幫助是極大的讚賞

謝謝 達米安

回答

8

你應該重構你的類我不能找到一種方式來獲得進入這個成爲另一個數據源的proxy,而不是從其中繼承。通過這種方式,您可以輕鬆地爲其注入一個模擬數據源,而不是真正的數據源。

import javax.sql.DataSource; 

public class TCSOracleDataSourceWrapper implements DataSource { 
    ... 
    private DataSource wrappedDataSource; 
    ... 

    public TCSOracleDataSourceWrapper(DataSource ds) { 
    wrappedDataSource = ds; 
    } 

    ... 

    public final Connection getConnection() throws SQLException { 
    ... 

    Connection connection = null; 
    try{ 
     connection = ds.getConnection();   
    } 
    catch(SQLException e) 
    { 
     ... 
    }  

    return connection; 
    } 
} 
3

想到一個想法:使用聚合而不是繼承。這個問題和其他人喜歡它會消失,因爲你可以嘲笑聚合的對象有任何你想要的行爲。我沒有看到另一種立即進入的方式。事實上,TCSOracleDataSourceWrapper這個名稱已經表明它包裝了一個數據源(聚合),而實際上它不是。

1

一個快速的解決方法是將super.getConnection()調用分解到新的private/protected方法。一旦你做出了改變,使用功能模擬來模擬getBaseConnection方法會很容易。這是短期修復,就像其他答案一樣,建議使用委託而不是繼承來包裝實現。

Connection getBaseConnection() throws SQLException { 
    return super.getConnection(); 
} 

public final Connection getConnection() throws SQLException { 

    logger.debug("Retrieving a database connection from the pool"); 

    Connection connection = null; 
    try{ 
     connection = getBaseConnection();   
    } 
    catch(SQLException e) 
    { 

     if(e.getErrorCode() == STALE_CONNECTION_EX_CODE) 
     {    
      logger.error("Stale Oracle connection found in the Connection Pool. Refreshing invalid DB connections."); 
      //refresh invalid connections 
      cacheManager.refreshCache(cacheName, OracleConnectionCacheManager.REFRESH_INVALID_CONNECTIONS); 
      //now try to get the connection again 
      connection = getBaseConnection(); 
     } 
     else 
     { 
      throw e; 
     } 
    }  
    return connection; 
}