Mockito - 调用私有方法的测试方法

时间:2015-07-03 08:33:29

标签: mockito criteria private-methods

我试图找到解决方案,但还没有找到。我试图测试公共方法,里面有几个私有的方法。私有方法通过泛型方法检索Hibernate的Criteria的问题之一,泛型方法又通过另一个泛型方法的链检索它。请看下面的代码。坦率地说,我不确定是否可以测试这种情况,但如果有人有想法,请建议他们:

ConcreteDao

public class ConcreteDao extends EntityDao<ConcreteEntity> {

    public Class<ConcreteEntity> getClassType() {
        return ConcreteEntity.class;
    }

}

EntityDao

public abstract class EntityDao<T> extends AbstractDao<T>{

    public List<T> getEntityByFilter(EntityFilter filter) {
        Criteria criteria = getCriteriaByFilter(filter.getFilters());
        criteria.setMaxResult(filter.getMaxResult());
        criteria.setFirstResult(filter.getFirstResult());
        criteria.addOrder(Order.asc(filter.getSortedField()));
        criteria.list();
    }

    private Criteria getCriteriaByFilter(List<CustFilter> filters) {
        Criteria criteria = getCriteria();
        for (CustFilter filter : filters) {
            filter.addrestrictionToCriteria(criteria, filter.getProperty(), filter.getValue());
        }
        return criteria;
    }

}

AbstractDao的

public abstract class AbstractDao<T> {

    private EntityManagerFactory entityManagerFactory;

    public abstract getClassType();

    public Criteria getCriteria() {
        return getSession().createCriteria(getClassType());
    }

    public Session getSession() {
        Session session = (Session) getEntityManager().getDelegate();
        return session;
    }

    public EntityManager getEntityManager() {
        entityManagerFactory.getEntityManager();
    }

}

测试类

@RunWith(MockitoJUnitRunner.class)
public class ConcreteDaoTest {

    @Mock
    private EntityManager entityManager;

    @Mock
    private Session session;

    @Mock
    private Criteria criteria;

    private List<CustFilter> filters;

    private EntityFilter entityFilter;

    private List<ConcreteEntity> resultList;

    @InjectMocks
    private ConcreteDao concreteDao = new ConcreteDao;

    public void init() {
        filters = new ArrayLis<CustFilter>();
        CustFilter custFilter = new CustFilter();
        //fill filter;
        filters.add(custFilter);

        entityFilter = new EntityFilter();
        //fill entityFilter
        entityFilter.setFilters(filters);

        ConcreteEntity concreteEntity = new ConcreteEntity();
        resultList = new ArrayList<ConcreteEntity>();
        resultList.add(concreteEntity);

    }


    @Test
    public void getEntityByFilterTest() {
        when(concreteDao.getEntityManager).thenReturn(entityManager);
        when(concreteDao.getSession()).thenReturn(session);
        when(concretedao.getCriteria()).thenReturn(criteria);

        when(filter.getFilters()).thenReturn(filters);
        when(filter.getMaxResult()).thenReturn(10);
        when(filter.getFirstResult()).thenReturn(0);
        when(filter.getSortedField()).thenReturn("firstName");
        when(criteria.list()).thenReturn(resultList);

        List<ConcreteEntity> result = concreteDao.getEntityByFilter(entityFilter);
        Assert.assertThen(result. is(notNullValue()));
    }

} 

1 个答案:

答案 0 :(得分:1)

使用Mockito,您无法模拟私有方法调用。 尝试使用PowerMockito模拟静态方法,私有方法,本地方法实例等任何类型的方法。