mock 私有方法
作者:互联网
UT代码:
@Test
public void test_private_getMenuTreeList() throws Exception {
AppMenuEntity entity = new AppMenuEntity();
entity.setType(Constant.MenuType.CATALOG.getValue());
AppMenuController spy = PowerMockito.spy(appMenuController);
PowerMockito.doReturn(Arrays.asList()).when(spy,"queryListParentId",Mockito.any(),Mockito.anyList());
List<AppMenuEntity> list = Whitebox.invokeMethod(spy,"getMenuTreeList",Arrays.asList(entity),Arrays.asList());
Assert.assertEquals(list.size(),1);
}
@Test
public void test_private_queryListParentId() throws Exception {
AppMenuEntity entity = new AppMenuEntity();
entity.setParentId(123L);
List<AppMenuEntity> list = Whitebox.invokeMethod(appMenuController,"queryListParentId",123L, Arrays.asList(entity));
Assert.assertEquals(list.size(),1);
}
目标代码:
/**
* 获取所有菜单列表
*/
private List<AppMenuEntity> getAllMenuList(List<AppMenuEntity> page){
//查询根菜单列表
List<AppMenuEntity> menuList = queryListParentId(0L, page);
//递归获取子菜单
getMenuTreeList(menuList, page);
return menuList;
}
private List<AppMenuEntity> queryListParentId(Long parentId, List<AppMenuEntity> page) {
List<AppMenuEntity> userMenuList = new ArrayList<>();
for(AppMenuEntity menu : page){
if(parentId==menu.getParentId()){
userMenuList.add(menu);
}
}
return userMenuList;
}
标签:spy,私有,List,entity,AppMenuEntity,queryListParentId,方法,page,mock 来源: https://blog.csdn.net/cq62wyf/article/details/121596498