我想从此SQL查询中模拟结果:

   public List<RiskFilters> findRiskFiltersByTerminalIdAndType(int terminal_id, String filter_class) {
        String hql = "....";
        TypedQuery<RiskFilters> query = entityManager.createQuery(hql, RiskFilters.class)....;
        List<RiskFilters> riskFilters = query.getResultList();
        return riskFilters;
    }


我尝试了这个样机配置:

List<RiskFilters> riskFiltersList = new ArrayList<RiskFilters>();
        riskFiltersList.add(new RiskFilters());

        when(riskFilterService.findRiskFiltersByTerminalIdAndType(anyInt(), anyString())).thenReturn(riskFiltersList);


但是我在这里得到了NPE:

List<RiskFilters> filter_list = riskFilterService.findRiskFiltersByTerminalIdAndType(11, "BinCountryCheckFilter");


看起来模拟的参数不正确。您知道如何正确实施吗?

最佳答案

我认为需要given(...).willReturn(...)

参考https://www.baeldung.com/bdd-mockito

10-07 16:33