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

前置步骤

引入依赖


    
        org.powermock
        powermock-module-junit4
        2.0.0
        test
    
    
        org.powermock
        powermock-api-mockito2
        2.0.0
        test
    

增加覆盖率扫描插件

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


    
        coverage
        
            true
        
        
            
                
                    org.jacoco
                    jacoco-maven-plugin
                    0.8.4
                    
                        
                            prepare-agent
                            
                                prepare-agent
                            
                        
                        
                            report
                            
                                report
                            
                        
                    
                
            
        
    

常见示例

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 中找到。

你可能感兴趣的:(后端)