当前位置:网站首页>Mockito3.8 如何mock静态方法 (如何mock PageHelper)
Mockito3.8 如何mock静态方法 (如何mock PageHelper)
2022-07-22 10:32:00 【dotaer-df】
项目中遇到需要mock PageHelper,因为用到了startPage方法,而此方法是静态方法,如果需要mock静态方法,网上说法比较多的都是需要用Powermock,而这就需要引入新的依赖,这样的话就比较臃肿了,那如何不引入新依赖,百搜不得其解,那如何解决这个问题呢?经过排查思考,如下。
- 升级org.mockito版本至3.8.0(3.4.0之前的版本不支持mock静态方法)
然后你就兴高采烈的去mock PageHelper了,如下面这样<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-junit-jupiter</artifactId> <version>3.8.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>3.8.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-inline</artifactId> <version>3.8.0</version> <scope>test</scope> </dependency>
你就会发现会抱这样的错Page<Plan> planPage = new Page<>(); MockedStatic<PageHelper> pageMethodMock = mockStatic(PageHelper.class); pageMethodMock.when(()->PageHelper.startPage(anyInt(),anyInt())).thenReturn(planPage);
rg.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced or misused argument matcher detected here:-> at so.dian.leto.service.PlanServiceTest.lambda$paging$0(PlanServiceTest.java:143)
-> at so.dian.leto.service.PlanServiceTest.lambda$paging$0(PlanServiceTest.java:143)You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
when(mock.get(anyInt())).thenReturn(null);
doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
verify(mock).someMethod(contains("foo"))This message may appear after an NullPointerException if the last matcher is returning an object
like any() but the stubbed method signature expect a primitive argument, in this case,
use primitive alternatives.
when(mock.get(any())); // bad use, will raise NPE
when(mock.get(anyInt())); // correct usage use
而正确做法应该是这样
MockedStatic<PageMethod> pageMethodMock = mockStatic(PageMethod.class);
pageMethodMock.when(()->PageHelper.startPage(anyInt(),anyInt())).thenReturn(planPage);
或者这样
MockedStatic<PageMethod> pageMethodMock = mockStatic(PageMethod.class);
pageMethodMock.when(()->PageMethod.startPage(anyInt(),anyInt())).thenReturn(planPage);
我们点开startPage方法,会发现这个方法是父类PageMethod的方法,PageHelper是继承而 来,所以我们必须要去Mock父类。
用完之后记得pageMethodMock.close()否则会出现这个错误
For com.github.pagehelper.page.PageMethod, static mocking is already registered in the current thread ,To create a new mock, the existing static mock registration must be deregistered
这是因为测试用例中多次mock了PageMethod
边栏推荐
- Classic cases of semaphore synchronization and mutual exclusion
- DNS劫持如何预防、DNS是什么?DNS劫持详解
- AMiner论文推荐
- The database is encapsulated by queryrunner simulation
- LeetCode53——Maximum Subarray——3 different methods
- LeetCode103——zigzagLevelOrder of binary tree
- Common tools for data development - regular sending of query results email
- 《预训练周刊》第38期: Transformer、BERT结构优化
- Comment le détournement de DNS peut - il être parfaitement réparé? Comment résoudre le problème du détournement de DNS
- Leetcode 22. bracket generation
猜你喜欢
网站安全之域名被劫持、域名被劫持后该怎么办!!!
Redis亿级数据存储方案哈希槽分区
Plug in installation of elastic search getting started (5)
LeetCode0003——longest substring without repeating characters——Sliding Window
网站莫名跳转,从百度谈什么是网站劫持?DNS劫持(域名劫持)DNS劫持是啥
LeetCode160 & LeetCode141——double pointers to solve the linked list
Introduction to elastic search: search full text search (7)
网站莫名跳转,从百度谈什么是网站劫持?百度快照劫持怎么解决
Introduction to machine learning: Logistic regression-2
Cmake+opencv+mingw
随机推荐
离线日志采集统计分析
Linux下安装mysql
她力量系列三丨把握当下,坚持热爱,与食物图像识别结缘的科研之路
The setting of node.master and node.data in the production environment of the introduction to elastic search (3)
网站莫名跳转,从百度谈什么是网站劫持?百度快照劫持怎么解决
笔记:C语言
LeetCode206——反转链表
dns劫持如何完美修複?dns被劫持如何解决如何完美修複
生成删除数据库所有表的外检脚本
Leetcode0022 - bracket generation - DFS
Cmake+opencv+mingw
Redis亿级数据存储方案哈希槽分区
16进制字符串与字节数组之间的转换
她力量系列六丨杨笛一:女孩长大后数理化可以很好,科研可以很鲜活
NC4 judge whether there is a ring in the linked list
docker搭建mysql主从复制
Guidelines for installation and use of Damon database
CSDN博客去除上传的图片水印
Introduction to machine learning: support vector machine-6
SSM框架整合