Java - code coverage

I have a method in one of the classes in my code base that for life I can’t enter in my unit tests. Basically this class is called when I request a connection to the database, if an obsolete connection is returned, a new connection is established.

Here is the mthod fragment in my class (cropped for this purpose)

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

Any idea how I can ensure that my junit tests execute an if statement? I am currently using EasyMock and Powermock, but I cannot find a way to get into this if statment using these tools

All help is appreciated

Thank. Damien

+5
source share
3 answers

, 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;
  }
}
+8

: , . , , , , . . , TCSOracleDataSourceWrapper , (), .

+3

One quick way to solve the problem is to call super.getConnection () for a new private / protected method. Once you make this change, it would be easy to make fun of the getBaseConnection method using power mock. This is a short-term fix, as other answers suggest that it is better to use delegation instead of inheritance to implement the shell.

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;
 }
+1
source

All Articles