https://ask.csdn.net/questions/8061230?weChatOA=weChatOA1
之前说可以使用拦截器来实现我的需求,但是当只有一个拦截声明时方法会校验为0的数据,返回期望值,然而其他传入参的查询都会返回空数组,为了实现后续逻辑,尝试使用多个拦截声明,
List<QueryData>list=new ArrayList<>();
when(queryMapper.selectByExample(argThat(new QueryExampleMatcher(0))).thenReturn(list);//只能用于单条件匹配
List<QueryData>list1=new ArrayList<>();
List<QueryData>list2=new ArrayList<>();
List<QueryData>list3=new ArrayList<>();
when(queryMapper.selectByExample(argThat(new QueryExampleMatcher(0))).thenReturn(list1);
when(queryMapper.selectByExample(argThat(new QueryExampleMatcher(1))).thenReturn(list2);
when(queryMapper.selectByExample(argThat(new QueryExampleMatcher(2))).thenReturn(list3);
但是第二个argThat(new QueryMatcher(1))方法会直接触发第一个argThat(new QueryMatcher(0))的模拟,进入matches方法中,然后由于example对象为空抛出空指针
我尝试了新建多个拦截器,
List<QueryData>list4=new ArrayList<>();
List<QueryData>list5=new ArrayList<>();
List<QueryData>list6=new ArrayList<>();
when(queryMapper.selectByExample(argThat(new QueryExampleMatcher0(0))).thenReturn(list4);
when(queryMapper.selectByExample(argThat(new QueryExampleMatcher1(1))).thenReturn(list5);
when(queryMapper.selectByExample(argThat(new QueryExampleMatcher2(2))).thenReturn(list6);
然而没有得到预期结果,测试类执行到argThat(new QueryExampleMatcher1(1))依旧会进入到QueryExampleMatcher0的matches方法,抛出空指针异常
我该如何解决这个问题