关于后端:要想学会-PowerMock-看这一篇文章就够了

43次阅读

共计 3017 个字符,预计需要花费 8 分钟才能阅读完成。

前置步骤

引入依赖

<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);
}

论断

以上代码都能够在 powermock-demo 中找到。

正文完
 0