简体   繁体   English

使用PowerMock-easymock模拟数据库对象

[英]Mocking of db objects using PowerMock-easymock

I have an Interface ShoppingListDAO as below. 我有如下界面ShoppingListDAO。

public interface ShoppingListDAO extends GenericDAO<Object, String> {       
    public List<ShoppingList> getShoppingList(Department department) throws ShoppingListDAOException;
}

And It's implementation DAO class is something like below one. DAO类的实现类似于下一个。

 public  class ShoppingListDAOImpl extends GenericCustomDAO<Object, String> implements ShoppingListDAO {
    //.......
    public  List<ShoppingList> getShoppingList(Department department)  throws ShoppingListDAOException {

    try {               
        ds = getDataSource();
        connection = ds.getConnection();

        callableStatment = connection.prepareCall(SHOPPING_LIST_QRY1);  
        callableStatment.setString(1, department.getDistributorNumber());
        //......    
        callableStatment.registerOutParameter(4, OracleTypes.CURSOR);

        callableStatment.execute();
        resultSet= (ResultSet) callableStatment.getObject(4);

        while(resultSet.next()) {
            //.......
        }           
    } catch (SQLException e) {          
        e.printStackTrace();
        throw new ShoppingListDAOException(e);
    } catch (Exception e) {         
        e.printStackTrace();
        throw new ShoppingListDAOException(e);
    }finally{
        //......                
    }   
}

    return shoppingList;
}

Now I have requirement to Test my implemented DAO class using Mock db Objects.I searched through POWERMOCK/EASYMOCK documentation but I guess most of the API methods provides me objects that provide me dummy implementation class of DAO interface. 现在我需要使用Mock db Objects测试我实现的DAO类。我搜索了POWERMOCK / EASYMOCK文档,但是我想大多数API方法都为我提供了一些对象,这些对象为我提供了DAO接口的虚拟实现类。

  1. is There some way i can create mock object of CONNECTION (assuming i don't have physical database access) and can run subsequent code provided in my ShoppingListDAOImpl class as i have to use this mocking for CODE COVERAGE purpose? 有什么方法可以创建CONNECTION的模拟对象(假设我没有物理数据库访问权限)并可以运行ShoppingListDAOImpl类中提供的后续代码,因为我必须将此模拟用于代码覆盖?

  2. if there is any way i can make callableStatement.execute() to return Dummy data or Exception(withour physical database access) so that i can check it in my JUnit test cases? 是否有任何方法可以使callableStatement.execute()返回虚拟数据或Exception(具有您的物理数据库访问权限),以便可以在JUnit测试用例中对其进行检查?

I am quite new to mocking framework ,So may be my requirements are unrealistic. 我对模拟框架很陌生,所以可能我的要求不切实际。 Any Information would be helpful. 任何信息都有帮助。

With mock frameworks like EasyMock, Powermock or Mockito, you can mock everything. 使用EasyMock,Powermock或Mockito等模拟框架,您可以模拟所有内容。 But I would not recommend to mock the Connection . 但是我不建议模拟Connection

In your case, I would use an in-memory database like HSQLDB to test your DAO against a real DB but a DB which runs in-memory. 在您的情况下,我将使用像HSQLDB这样的内存数据库来测试您的DAO是否针对实际的DB,而是针对在内存中运行的DB。 This way, your test are not dependent of any external resources and can easily run on any environment. 这样,您的测试不依赖任何外部资源,并且可以轻松地在任何环境下运行。

By testing your code against a database, you are strictly not unit testing it anymore. 通过针对数据库测试代码,您将不再严格进行单元测试。 Though I think this is an acceptable tradeoff. 尽管我认为这是可以接受的折衷方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM