powerMock和mockito

  • powermock和mockito都是做mock的框架,powermock在mockito的基础上扩展而来,支持mockito的操作(也支持别的mock框架比如easyMock)。因此在maven引入powermock的时候,需要引mockito的包。powermock和mockito版本上要配合着使用。powermock在mockito的基础上,扩展了对static class, final class,constructor,private method等的mock操作。慎用这些mock,因为在一个良好的设计里,static final private这些class和method是不需要被测试的,会被public方法调用,只要测试public就好。

使用

  • maven引入
<properties>
<powermock.version>2.0.2</powermock.version>
</properties>
<dependencies>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
  • 和spring配合使用的时候,要特别注意版本的问题,如果测试起不来的话,先确认下powermock的版本和spring是否对应。

注解

  • @powermockIgnore,默认情况下,powermock试图使用自己的classLoader去loader所有的class,除里system class(java.lang等目录下的class),使用powermockIgnore声明的class,pwoermock也不会加载。

白盒测试

  • powermock提供了几种方法来处理私有方法,私有变量,私有构造函数
  • Use Whitebox.setInternalState(..) to set a non-public member of an instance or class.
  • Use Whitebox.getInternalState(..) to get a non-public member of an instance or class.
  • Use Whitebox.invokeMethod(..) to invoke a non-public method of an instance or class.
  • Use Whitebox.invokeConstructor(..) to create an instance of a class with a private constructor.

mock构造函数

whenNew(MyClass.class).withNoArguments().thenThrow(new IOException("error message"));

@RunWith(PowerMockRunner.class)
@PrepareForTest(X.class)
public class XTest {
@Test
public void test() {
whenNew(MyClass.class).withNoArguments().thenThrow(new IOException("error message")); X x = new X();
x.y(); // y is the method doing "new MyClass()" ..
}
}
  • prepare的时候,prepareForTest的类是调用MyClass的类。

Delegate to another JUnit Runner

  • spring和powermock结合使用时,使用以下联合注解
@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@PrepareForTest(Test.class)
  • powerMock还是由自己的runner来做object的mock工作,在执行时,再交给delegate的runner去执行。
  • 不同的runner结合使用https://codete.com/blog/testing-spring-boot-application-with-junit-and-different-runners/

各种runner是何时如何起作用的呢

  • 首先runner是干啥的?

    • runner其实就是各个框架在跑测试case的前后处理一些逻辑。
    • 比如在Junit框架中,我们什么都不写,用Junit默认的Runner BlockJUnit4ClassRunner来执行case,主要做什么事情呢?就是处理Junit框架中的一些注解,比如扫到那些所有@Test的注解,这些是要跑的case,将那些@Ignore的注解的case忽略掉,在执行test case的前后,执行那些@Before和@after的注解的方法。
  • BlockJUnit4ClassRunner
 @Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description = describeChild(method);
if (method.getAnnotation(Ignore.class) != null) {
notifier.fireTestIgnored(description);
} else {
runLeaf(methodBlock(method), description, notifier);
}
} @Override
protected Description describeChild(FrameworkMethod method) {
return Description.createTestDescription(getTestClass().getJavaClass(),
testName(method), method.getAnnotations());
} @Override
protected List<FrameworkMethod> getChildren() {
return computeTestMethods();
}
  • 主要是这三个方法,getChildren得到所有@Test注解的方法。runChild中,将要调用的方法组织好,最后通过反射调用这个方法执行,同时处理执行成功或者执行失败的结果。

  • mockito的runner,JUnit44RunnerImpl,在跑test之前,将@Mock注解的对象构造出来。

  • SpringJUnit4ClassRunner 在test class中做依赖注入。

  • 总之,就是各个不同的runner在处理各自框架的职责。mockitorunner的就是负责mock,spring的runner就是负责依赖注入。

  • 这里也就解释了powermock的delegate是如何work的: testClass首先会交给powermockRunner完成自己的mock的工作,然后再交给springRunner去完成依赖注入的工作。

  • https://dzone.com/articles/understanding-junits-runner

  • https://codete.com/blog/testing-spring-boot-application-with-junit-and-different-runners/

参考

powerMock和mockito使用的更多相关文章

  1. Mock之easymock, powermock, and mockito

    easymock, powermock, and mockito Easymock Class Mocking Limitations To be coherent with interface mo ...

  2. 简单介绍如何使用PowerMock和Mockito来mock 1. 构造函数 2. 静态函数 3. 枚举实现的单例 4. 选择参数值做为函数的返回值(转)

    本文将简单介绍如何使用PowerMock和Mockito来mock1. 构造函数2. 静态函数3. 枚举实现的单例4. 选择参数值做为函数的返回值5. 在调用mock出来的方法中,改变方法参数的值 一 ...

  3. Spring Boot 2 实践记录之 使用 Powermock、Mockito 对 UUID 进行 mock 单元测试

    由于注册时,需要对输入的密码进行加密,使用到了 UUID.sha1.md 等算法.在单元测试时,使用到了 Powermock,记录如下. 先看下加密算法: import org.apache.comm ...

  4. java.lang.AbstractMethodError: org.powermock.api.mockito.internal.mockmaker.PowerMockMaker.isTypeMockable

    [转]https://stackoverflow.com/questions/53539930/java-lang-abstractmethoderror-org-powermock-api-mock ...

  5. 使用Powermock和mockito来进行单元测试

    转载:http://blog.csdn.net/u013428664/article/details/44095889 简介 Mockito是一个流行的Mocking框架.它使用起来简单,学习成本很低 ...

  6. 使用MRUnit,Mockito和PowerMock进行Hadoop MapReduce作业的单元测试

    0.preliminary 环境搭建 Setup development environment Download the latest version of MRUnit jar from Apac ...

  7. dubbo应用程序的单元测试环境搭建(springtest,powermock,mockito)

    转:http://blog.csdn.net/yys79/article/details/66472797 最近,项目中频繁用到dubbo,而且java工程用引用了几十个关联系统的服务(如用户认证,基 ...

  8. springboot集成mockito与powermock

      mockito大家都比较熟悉了,存在或者不存在,都不要紧,mockito让你有一种只要一出手,就知道有没有的感觉.但是它也不是万能的,比如静态方法.私有方法,它就无能为力了.这是为什么呢?当然不是 ...

  9. 单元测试及框架简介 --junit、jmock、mockito、powermock的简单使用

    转 单元测试及框架简介 --junit.jmock.mockito.powermock的简单使用 2013年08月28日 14:33:06 luvinahlc 阅读数:6413 标签: 测试工具单元测 ...

随机推荐

  1. Git 推送到远程仓库

    github:https://github.com/ 国内的:https://gitee.com/ (和Github非常相似的) 一.Http方式进行推送 右击同步,配置远端,将URL替换成远程仓库的 ...

  2. 前端学习(五):body标签(三)

    进击のpython ***** 前端学习--body标签 接下来的内容就比较多了,各位看官且听我慢慢道来... ... 使用a标签,链接到另一个页面 网页中<a>标签,全称:anchor. ...

  3. Kaggle 入门题-泰坦尼克号灾难存活预测

    这个题目的背景概况来讲就是基于泰坦尼克号这个事件,然后大量的人员不幸淹没在这个海难中,也有少部分人员在这次事件之中存活,然后这个问题提供了一些人员的信息如姓名.年龄.性别.票价,所在客舱等等一些信息, ...

  4. jmeter混合场景的多种实现方式比较

    性能测试设计混合场景,一般有几种方式,分别是每个场景设置一个线程组,使用if控制器,使用吞吐量控制器.不同的方式实现机制不一样,哪种方式相比而言更好呢?下面做一比较. 下面以混合访问百度首页和必应首页 ...

  5. Mybatis开启二级缓存(全局缓存)的方法

    Mybatis开启二级缓存的方法 开启步骤 1.在 mybatis-config.xml 的配置文件中进行显示配置,开启二级缓存(全局缓存) 2.在 Mapper.xml 文件中添加cache标签 一 ...

  6. 遍历多个 txt 文件进行获取值

    import random def load_config(path): with open(path,'r') as tou: return [line for line in tou.readli ...

  7. 使用Esxi虚拟化部署OpenWrt/HomeLede+扩容硬盘 保姆级教程

    本文介绍使用VMware虚拟化平台部署OpenWrt/HomeLede,并扩容固件硬盘的方法. 推荐使用虚拟化方式部署软路由,理由如下: 部署.升级.回退.扩容等操作非常方便,特别适合折腾 可以方便的 ...

  8. PHP preg_last_error() 函数

    preg_last_error 函数用于返回最后一个 PCRE 正则执行产生的错误代码.高佣联盟 www.cgewang.com 语法 int preg_last_error ( void ) 实例 ...

  9. C/C++编程笔记:《C语言》—— 数组知识详解,学编程建议收藏!

    作者:龙跃十二链接:https://www.imooc.com/article/300814 ,微信公众号:龙跃十二 数组的基本概念 我们把一组数据的集合称为数组(Array),它所包含的每一个数据叫 ...

  10. 深入探究JVM之类加载与双亲委派机制

    @ 目录 前言 类的生命周期 加载 验证 准备 解析 初始化 案例一 案例二 案例三 案例四 类加载器 类加载器和双亲委派模型 破坏双亲委派模型 第一次 SPI Tomcat OSGI 总结 前言 前 ...