为了账号安全,请及时绑定邮箱和手机立即绑定

要想学会 PowerMock 看这一篇文章就够了

标签:
测试

前置步骤

引入依赖

<dependencies>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-module-junit4</artifactId>
        <version>2.0.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.powermock</groupId>
        <artifactId>powermock-api-mockito2</artifactId>
        <version>2.0.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

增加覆盖率扫描插件

项目需要被扫描到,需要在项目外层 pom 文件添加如下配置:

<profiles>
    <profile>
        <id>coverage</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.jacoco</groupId>
                    <artifactId>jacoco-maven-plugin</artifactId>
                    <version>0.8.4</version>
                    <executions>
                        <execution>
                            <id>prepare-agent</id>
                            <goals>
                                <goal>prepare-agent</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>report</id>
                            <goals>
                                <goal>report</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

常见示例

Mock 类自己的公有方法

@Test
public void testMockPublicMethod(){
    UserServiceImpl userService1=PowerMockito.spy(userServiceImpl);
    PowerMockito.doReturn("mockPublicMethod").when(userService1).publicMethod(any());
    String result=userService1.mockPublicMethod(new UserBO());
    Assert.assertEquals("mockPublicMethod",result);
}

Mock 类自己的 Final 公有方法

@Test    
public void testMockPublicFinalMethod(){
    UserServiceImpl userService1=PowerMockito.spy(userServiceImpl);
    PowerMockito.doReturn("mockFinalPublicMethod").when(userService1).finalPublicMethod(any());
    String result=userService1.mockFinalPublicMethod(new UserBO());
    Assert.assertEquals("mockFinalPublicMethod",result);
}  

Mock 类自己的私有方法

@Test
public void testMockPrivateMethod()throws Exception{
    UserServiceImpl userService1=PowerMockito.spy(userServiceImpl);
    PowerMockito.doReturn("mockPrivateMethod").when(userService1,"privateMethod",any());

    String result=userService1.mockPrivateMethod(new UserBO());
    Assert.assertEquals("mockPrivateMethod",result);
}

Mock 类自己的私有静态方法

@Test
public void testMockPrivateStaticMethod()throws Exception{
    PowerMockito.mockStatic(UserServiceImpl.class);
    PowerMockito.doReturn("mockPrivateStaticMethod").when(UserServiceImpl.class,"privateStaticMethod",any());

    String result=userServiceImpl.mockPrivateStaticMethod(new UserBO());
    Assert.assertEquals("mockPrivateStaticMethod",result);
}

Mock 类自己的公有静态方法

@Test
public void testMockPublicStaticMethod(){
    PowerMockito.mockStatic(UserServiceImpl.class);
    PowerMockito.when(UserServiceImpl.publicStaticMethod(any())).thenReturn("mockPublicStaticMethod");

    String result=userServiceImpl.mockPublicStaticMethod(new UserBO());
    Assert.assertEquals("mockPublicStaticMethod",result);
}

Mock 父类的方法

@Test
public void testMockParentMethod() {
    UserServiceImpl userService1 = PowerMockito.spy(userServiceImpl);
    PowerMockito.doReturn("mockParentMethod").when(userService1).parentMethod(any());

    String result = userService1.mockParentMethod(new UserBO());
    Assert.assertEquals("mockParentMethod", result);
}

Mock 依赖的对象方法

@Test
public void testMockMapper() {
    UserDO userDO = new UserDO();
    userDO.setName("mockMapper");
    PowerMockito.when(userMapper.getById(anyLong())).thenReturn(userDO);
    String result = userServiceImpl.getUserName(1000L);
    Assert.assertEquals("mockMapper", result);
}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消