mock中测试private方法,不是mock

Method method = PowerMockito.method(CategoryController.class, "getCategory",List.class);//创建调用CategoryController类中的getCategory私有方法的method对象,参数是list对象
List<Category> category_all_actual = (List<Category>)method.invoke(categoryController, categories);//调用categoryController的私有方法,返回list对象,参数是list对象
assertArrayEquals(category_all_expect.toArray(), category_all_actual.toArray());//比较实际返回的对象与期望的对象是否相等.

或者:// PowerMockito.doReturn(index_expect).when(controller, "processPage", pageCode, request, response, model); // Mock私有方法

或者:// PowerMockito.when(controller, "processPage", pageCode, request, response, model).thenReturn(index_expect); // Mock私有方法

原文地址:https://www.cnblogs.com/nizuimeiabc1/p/6483016.html