2011-09-01 145 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; 
}