static修饰的方法如何mock
时间: 2023-08-29 09:10:29 浏览: 138
引用\[1\]中提到,对于final方法的mock,需要使用PowerMockito.mock而不是mockito.mock。因为在mock对象的构造和方法mock的过程中,需要使用PowerMockito来支持final方法的mock。而在引用\[2\]中,使用了mockstatic的方法来准备了StudentUtils工具类,并使用powermock.verifyStatic()来验证。这体现了PowerMock的强大之处。对于仅有static方法的情况,可以使用PowerMockRunner和PowerMockito.mockStatic来进行mock。在测试类上使用@RunWith(PowerMockRunner.class)注解,并使用@PrepareForTest注解来指定需要mock的类。然后使用PowerMockito.mockStatic来mock静态方法。例如,在测试类ConditionServiceImplTest中,使用了PowerMockRunner和PowerMockito.mockStatic来mock SpringContextUtil类的静态方法。\[3\]
#### 引用[.reference_title]
- *1* [Spock单元测试-mock final方法(普通类中的final方法)](https://blog.csdn.net/m0_46420685/article/details/125527525)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [PowerMock学习(四)之Mock static的使用](https://blog.csdn.net/weixin_39854730/article/details/113023416)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [Mock带有static方法](https://blog.csdn.net/leave417/article/details/125112749)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文