Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java代码覆盖率_Java_Unit Testing_Code Coverage - Fatal编程技术网

Java代码覆盖率

Java代码覆盖率,java,unit-testing,code-coverage,Java,Unit Testing,Code Coverage,我在代码库中的一个类中有一个方法,在我的一生中,我无法进入junit测试。 基本上,当我请求数据库连接时会调用这个类,如果返回一个过时的连接,就会建立一个新的连接 这是我的类中的方法的片段(为了达到这个目的而删减) 知道如何确保junit测试执行if语句吗? 我目前正在使用EasyMock和Powermock,但如果使用这些工具,我找不到一种方法来解决这个问题 非常感谢您的帮助 多谢各位 Damien您应该重构类,使其成为另一个数据源的源,而不是从一个数据源继承。通过这种方式,您可以轻松地向其中

我在代码库中的一个类中有一个方法,在我的一生中,我无法进入junit测试。 基本上,当我请求数据库连接时会调用这个类,如果返回一个过时的连接,就会建立一个新的连接

这是我的类中的方法的片段(为了达到这个目的而删减)

知道如何确保junit测试执行if语句吗? 我目前正在使用EasyMock和Powermock,但如果使用这些工具,我找不到一种方法来解决这个问题

非常感谢您的帮助

多谢各位
Damien

您应该重构类,使其成为另一个数据源的源,而不是从一个数据源继承。通过这种方式,您可以轻松地向其中注入模拟数据源,而不是真实的数据源

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

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

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