記錄powermock的使用例子

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) {
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章