1.mock私有方法, 同時注入mock對象
InvoiceApplyServiceImpl服務待測試的方法中包含getHistory私有方法,同時包含注入的dao查詢方法。
@InjectMocks private ApplyServiceImpl applyService; @Mock private InformationDao informationDao; ApplyServiceImpl applyServiceSpy = PowerMockito.spy(applyService); PowerMockito.doReturn(applyOptional).when(applyServiceSpy, "getHistory", any(),Mockito.anyLong());
InformationList<> list = new ArrayList(); when(informationDao.query(Mockito.any())).thenReturn(informationList);
2.mock final類包含的靜態方法
PowerMockito.mockStatic(InfoConfig.class);
PowerMockito.when(InfoConfig.getInfo()).thenReturn(Info);
3.測試私有方法- 通過反射工具
ReflectionTestUtils.invokeMethod public static <T> T invokeMethod(Object target, String name, Object... args) {