Java - 代码覆盖率

时间:2011-09-01 16:20:11

标签: java unit-testing code-coverage

我在我的代码库中的一个类中有一个方法,对于我的生活,我无法进行我的junit测试。 基本上,当我请求数据库连接时调用此类,如果返回陈旧连接,则建立新连接

以下是我班级中的方法片段(为此目的进行了修剪)

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;
}}

知道如何确保我的junit测试执行if语句吗? 我目前正在使用EasyMock和Powermock,但如果使用这些工具进行判断,我找不到进入此方法的方法

非常感谢所有帮助

谢谢 达明

3 个答案:

答案 0 :(得分: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;
  }
}

答案 1 :(得分:3)

我想到一个想法:使用聚合而不是继承。这个问题和其他类似的问题会消失,因为你可以模拟聚合对象以获得你想要的任何行为。我没有看到另一种方式进入那里。事实上,名称TCSOracleDataSourceWrapper已经表明它正在包装数据源(聚合),而实际上并非如此。

答案 2 :(得分:1)

一个快速的解决方法是将super.getConnection()调用分解为新的private / protected方法。一旦进行了更改,就可以很容易地使用power mock来模拟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;
 }