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私有方法get

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

相關文章
相關標籤/搜索