如何使用Powermock-easymock从API方法返回异常

时间:2013-07-19 10:35:08

标签: java

我是EasyMock / PowerMock的初学者所以如果有人可以帮助我解决以下问题。 我有以下DAOImpl类,其中我连接到数据库并执行搜索操作。

public  List<ShoppingList> getShoppingList(Department department)  throws ShoppingListDAOException {

        SHOPPING_LIST_QRY1= "{call ec_list_package.get_shopping_lists_id_order(?,?,?,?)}";
        connection =null;
        ResultSet resultSet =null;
        CallableStatement  callableStatment = null;
        List <ShoppingList> shoppingList = new ArrayList<ShoppingList>();       

        DataSource ds = null;   
        try {           

            if("true".equals(junit)){
            connection = getDataSource(true);
            }else {
                ds = getDataSource();
                connection = ds.getConnection();
            }   

            callableStatment = connection.prepareCall(SHOPPING_LIST_QRY1);  
            callableStatment.setString(1, department.getDistributorNumber());
            callableStatment.setString(2, department.getCustomerNumber());
            callableStatment.setString(3, department.getDepartmentNumber());    
            callableStatment.registerOutParameter(4, OracleTypes.CURSOR);
            callableStatment.execute();
            resultSet= (ResultSet) callableStatment.getObject(4);

            while(resultSet.next()) {
                           .......some code here.....               
            }           
        } catch (SQLException e) {          
            e.printStackTrace();
            throw new ShoppingListDAOException(e);
        } catch (Exception e) {         
            e.printStackTrace();
            throw new ShoppingListDAOException(e);
        }finally{
            try {
                if(resultSet!=null)
                    resultSet.close();
                if(callableStatment!=null)
                    callableStatment.close();
                if(connection!=null)
                    connection.close();
            } catch (SQLException e) {              
                e.printStackTrace();                
        }   
    }           
        return shoppingList;
    }
  1. 现在有什么方法可以使用Mocking从callableStatement.execute()方法返回Exception对象,以提高我的代码覆盖率。

0 个答案:

没有答案