PowerMock使用
来源:互联网 发布:ubuntu装搜狗输入法 编辑:程序博客网 时间:2024/04/26 17:28
注:这篇文档结合PowerMock一起学习,这里只介绍使用。
- 使用心得
@RunWith(PowerMockRunner.class)@PrepareForTest({Configuration.class})public class ConWendaDataResourceTest { 注:@InjectMock的作用是当我们@Mock的对象需要在其他类的方法中使用时,可以自动将Mock的对象注入到@InjectMock注解类的方法中。如我们Mock的courseRepository对象在ConWendaDataServiceImpl的方法中需要使用时就需要用@InjectMock注解。 @InjectMocks private ConWendaDataServiceImpl conWendaDataServiceImpl=new ConWendaDataServiceImpl(); 注:@Mock的作用是自动进行PlatformUserDetails user=PowerMockito.mock(PlatformUserDetails.class); 省去了在代码中PowerMockito.mock()。但是静态类还需要显示声明:PowerMockito.mockStatic(Configuration.class); @Mock private ICourseRepository courseRepository; @Mock private PlatformUserDetails user; @Test public void wendaTabTest() throws Exception { long userId=4874546; Set<String> courseIdsSet=new HashSet<String>(); courseIdsSet.add("235"); courseIdsSet.add("236"); courseIdsSet.add("237"); courseIdsSet.add("238"); courseIdsSet.add("239");//省去了显示声明: PlatformUserDetails user=PowerMockito.mock(PlatformUserDetails.class); PowerMockito.when(user.getUserId()).thenReturn(Long.valueOf(4874546)); PowerMockito.when(user.getLoginName()).thenReturn("");// ICourseRepository courseRepository=PowerMockito.mock(CourseRepositoryImpl.class);//注:getUserLearnedCourseIds方法就是@InjectMocks注解类中的方法。 PowerMockito.when(courseRepository.getUserLearnedCourseIds(String.valueOf(userId))).thenReturn(courseIdsSet);注:Configuration静态类还需要显示声明。 PowerMockito.mockStatic(Configuration.class); PowerMockito.when(Configuration.getArray("")).thenReturn(new String [] {""}); System.out.println(conWendaDataServiceImpl.getWendaTabDataFromWenda(user,0,10)); }}
- 使用心得
@RunWith(PowerMockRunner.class)@PrepareForTest({ElementServiceImpl.class, PlatformSecurityUtils.class, ElementConvert.class, ObjectMapper.class, Configuration.class, ConWendaDataServiceImpl.class})public class ConWendaDataServiceImplTest { @InjectMocks private IElementService elementService = PowerMockito.spy(new ElementServiceImpl()); @InjectMocks private IConWendaDataService conWendaDataService = PowerMockito.spy(new ConWendaDataServiceImpl()); @Mock private ElementMapper elementMapper; /** * 用于在service层调用courseMapper的时候使用 */ @Mock private CourseMapper courseMapper; @Test public void testCreateElement() throws Exception { Map<String, Object> params = new HashMap<String, Object>(); params.put("name", "name"); params.put("description", "123"); Element element = elementService.createElement(params); assertNotNull(element); } /** * 对问答接口进行测试 * * @throws Exception */ @Test public void wendaTabTest() throws Exception { /** * 获取邀请回答信息测试 */ System.out.println(conWendaDataService.getAnswerPeopleInfo(Long.valueOf("265"))); }}
0 0
- PowerMock使用
- 使用Powermock和mockito
- powermock
- powermock
- powermock
- PowerMock与Mockito的使用
- 使用PowerMock来Mock静态函数
- 使用PowerMock过程中的问题及解决
- 使用Powermock和Mockito测试静态方法
- 使用 PowerMock 以及 Mockito 实现单元测试
- 使用Powermock和mockito测试静态方法
- 使用Powermock实现单元测试,提高单元测试覆盖率
- 使用Powermock和mockito来进行单元测试
- 使用Powermock和mockito来进行单元测试
- 使用PowerMock和Easymock进行单元测试
- 使用Powermock实现单元测试,提高单元测试覆盖率
- 如何使用PowerMock帮助做TDD?
- powermock 入门介绍及使用示例
- Android 项目中遇到的坑,特此记录
- oracle间断性走错执行计划
- Error:Execution failed for task ':app:buildInfoDebugLoader'. > Exception while doing past iteration
- IO流复制文件夹
- powerdesigner配置 - (1)
- PowerMock使用
- Codeforces 685B 树形dp
- iOS GPUImage之GPUImageFilterPipeline组合滤镜(4)
- 关于使用FTP上传文件到服务器的小总结-java
- hdu 4989 Summary(水题)
- struts2的分页,使用迭代器标签<s:iterator
- gdb调试命令的使用及总结
- Python中模块之间变量引用的方法说明
- Computational Motif Finding