旅人葉 2024-01-31 16:26 采纳率: 100%
浏览 1
已结题

使用ArgumentMatcher 来实现mock模拟不同查询条件的selectByExample请求 junit4mockito.java.ft

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方法,抛出空指针异常
我该如何解决这个问题

  • 写回答

4条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2024-01-31 16:28
    关注

    参考通义千问和郭老师的小迷弟雅思莫了-编写提供,主要是为了帮助您快速解决问题,如下回答,您可以参考下,如有问题,评论区留言。

    Mockito的ArgumentMatcher用于根据传入参数进行条件匹配,以返回不同的模拟结果。针对您遇到的问题,Mockito在执行时会从上到下查找匹配的when-thenReturn规则,一旦找到匹配项就会停止查找。

    问题在于您的Matcher实现可能没有正确地区分不同查询条件,导致所有情况都进入了相同的matches方法,并抛出了NullPointerException。

    为了修复这个问题,您需要确保在QueryExampleMatcher中正确地比较和匹配Example对象的属性值:

    class QueryExampleMatcher extends ArgumentMatcher<Example> {
        private final int expectedValue; // 假设这个是你要匹配的某个属性值
    
        public QueryExampleMatcher(int expectedValue) {
            this.expectedValue = expectedValue;
        }
    
        @Override
        public boolean matches(Example argument) {
            if (argument == null) {
                return false; // 避免空指针异常
            }
            // 获取Example中的实际值,这里假设example.getCriterion()可以获取到对应的属性值
            int actualValue = ((Equals Criterion) argument.getCriterion()).getValue();
            return actualValue == expectedValue;
        }
    }
    
    // 然后在mock设置中使用
    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);
    
    

    请根据实际情况调整matches方法内的逻辑,确保它可以准确地区分并匹配不同的查询条件。同时,如果Example对象的结构较为复杂,可能还需要进一步解析其内部的Criterion或PropertySelector等信息来完成匹配。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 2月8日
  • 已采纳回答 1月31日
  • 创建了问题 1月31日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题