关于springboot:JSON-path-expectedentity-but-wasparams

8次阅读

共计 2771 个字符,预计需要花费 7 分钟才能阅读完成。

前言

人不知; 鬼不觉假期就完结了,感觉在家的效率比在学校差太多,在学校晚睡,然而能早起,在家想早起是不可能的了,在家有着各种事件的影响,在学校就没有太多影响,回到学校还是很兴奋的。
在本周学习的过程中遇到了这样的问题:

java.lang.AssertionError: JSON path "$.menus" expected:<[club.yunzhi.exam.entity.Menu@5e54c34b]> but was:<{id=-5936975678746039928, name=CgIV, deleted=false, url=1Dzo, roleName=71qs}>
Expected :[club.yunzhi.exam.entity.Menu@5e54c34b]
Actual   :{id=-5936975678746039928, name=CgIV, deleted=false, url=1Dzo, roleName=71qs}

解决

本问题呈现在后盾 C 层数据断言的测试中,代码如下:

    @Test
    void getRoleById() throws Exception{Long id = new Random().nextLong();
        Role role = new Role();
        Set<Menu> menus = new HashSet<>();
        Menu menu = new Menu();
        menu.setId(new Random().nextLong());
        menu.setName(new RandomString().make(4));
        menu.setRoleName(new RandomString().make(4));
        menu.setUrl(new RandomString().make(4));
        menus.add(menu);
        role.setId(id);
        role.setName(RandomString.make(4));
        role.setMenus(menus);

        Mockito.doReturn(role).when(this.roleService).getRoleById(Mockito.eq(id));

        String url = this.url + "/" + id.toString();
        System.out.println(MockMvcResultMatchers.jsonPath("$.menus"));
        System.out.println(role.getMenus());
        this.mockMvc.perform(MockMvcRequestBuilders.get(url))
                .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(role.getId()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.name").value(role.getName()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.menus").value(role.getMenus()));
    }

而后起初是在 Debug 模式下进行察看:

原本还想着在 ResultMatchers 离面看一下参数,怎奈外面啥也没有。
而后再瞅一下报错信息:冀望的事一个实体数组,实际上却是实体对象,而后换了一种思路:

    void getRoleById() throws Exception{Long id = new Random().nextLong();
        Role role = new Role();
        Set<Menu> menus = new HashSet<>();
        Menu menu = new Menu();
        menu.setId(new Random().nextLong());
        menu.setName(new RandomString().make(4));
        menu.setRoleName(new RandomString().make(4));
        menu.setUrl(new RandomString().make(4));
        menus.add(menu);
        role.setId(id);
        role.setName(RandomString.make(4));
        role.setMenus(menus);

        Mockito.doReturn(role).when(this.roleService).getRoleById(Mockito.eq(id));

        String url = this.url + "/" + id.toString();
        System.out.println(MockMvcResultMatchers.jsonPath("$.menus"));
        System.out.println(role.getMenus());
        this.mockMvc.perform(MockMvcRequestBuilders.get(url))
                .andExpect(MockMvcResultMatchers.jsonPath("$.id").value(role.getId()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.name").value(role.getName()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.menus[0].id").value(menu.getId()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.menus[0].name").value(menu.getName()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.menus[0].roleName").value(menu.getRoleName()))
                .andExpect(MockMvcResultMatchers.jsonPath("$.menus[0].url").value(menu.getUrl()));
    }



既然实体数组外面的实体没方法间接获取,咱们能够获取它的属性值,而后同咱们给 menu 实体各个属性的值进行比对。![image.png](/img/bVbLHCx)
运行胜利。## 总结
在解决问题的过程中,要关注控制台给的报错信息,否则会走弯路,耽搁好多工夫,这种解决办法尽管通过了测试,但绝不是最好的解决办法,限于笔者程度,没有方法间接获取实体进行比照,待当前想到更好的方法后再更新本文吧,感激 [凯强](https://segmentfault.com/u/zhaokaiqiang) 同学在解决问题的过程中给予帮助。* * *
正文完
 0