Mockito的使用

依赖

		<dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.9</version> 
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.9</version>
        </dependency>

示例代码

public class StaticUtils {
    public static String getPublicStatic() {
        return "publicStatic";
    }
}
public class MyService {
    @Resource
    InnerService innerService;

    public String getPublic() {
        return innerService.getPublicInner();
    }
    public String getStatic() {
        return StaticUtils.getPublicStatic();
    }
    public String getPrivate() {
        return privateMethod();
    }
    private String privateMethod() {
        return "private";
    }

    public Entity getEntity() {
        return new Entity("serviceEntity");
    }
    public void noArgs(){
        noArgPublic();
        System.out.println("noArgs");
    }
    public void noArgPublic() {
        System.out.println("noArgPublic");
    }
}
@RunWith(PowerMockRunner.class)
@PrepareForTest({StaticUtils.class})// 静态方法需要准备
public class MyServiceTest {
    //完整使用测试类时使用该注解,需要部分修改使用spy,需要完全mock就使用mock
    @InjectMocks
    MyService myService;

//    @Mock(name = "service") 内部属性使用名称注入简便方法
    @Mock
    InnerService innerService;

    @Before
    public void setUp() throws Exception {
//        Whitebox.setInternalState(myService,"service",innerService);  //内部属性使用名称注入
        PowerMockito.mockStatic(StaticUtils.class);//静态方法需要准备
    }

    //当前使用junit4 org.junit.Test;
    //使用junit5的注解会导致识别失败,报 No tests found matching Method 的错误
    @Test
    //mock注入类的public方法
    public void testPublic() {
        PowerMockito.when(innerService.getPublicInner()).thenReturn("mockPublic");
        System.out.println(myService.getPublic());

        PowerMockito.when(StaticUtils.getPublicStatic()).thenReturn("mockStatic");
        System.out.println(myService.getStatic());
    }

    @Test
    //mock私有方法
    public void testPrivate() throws Exception {
        MyService myServiceSpy = PowerMockito.spy(myService);//获取spy对象,当想mock测试类方法时可使用
        PowerMockito.doReturn("mockPrivate").when(myServiceSpy,"privateMethod");
        System.out.println(myServiceSpy.getPrivate());
    }

    @Test
    //mock构造方法
    public void testConstructor() throws Exception {
        Entity mockEntity = new Entity("mockEntity");
        PowerMockito.whenNew(Entity.class).withArguments(Mockito.anyString()).thenReturn(mockEntity);
        System.out.println(myService.getEntity().getName());
    }

    @Test
    public void testNoArgs(){
        MyService myServiceSpy = PowerMockito.spy(myService);
        Mockito.doNothing().when(myServiceSpy).noArgPublic();
        myServiceSpy.noArgs();
    }
}

注意事项

当前使用junit4 org.junit.Test;使用junit5的注解会导致识别失败,报 No tests found matching Method 的错误

使用jacoco时覆盖率不准确,maven日志报a different version of class was executed at runtime ;
for report generation the same class files must be used as at runtime
是因为在@PrepareForTest加了当前测试类

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值