前言
如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!
单元测试(mock) 错误org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;解析
01 准备
01::01 安装maven dependency helper插件和maven helper插件
用来分析你的依赖
打开idea 选择 File -> Settings… ->Plugins
输入maven dependency helper
选择下面红框处的两个插件安装,用来分析
02 问题经过
依赖:
<!-- PowerMock+Mockito+Junit4.4 -Above-->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.0-RC.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.9</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>2.15.0</version>
</dependency>
使用:
@InjectMocks
TestController controller;
@Mock
private TestService service;
@Before
public void setUp() {
//为UT提供框架使用的自动验证
MockitoAnnotations.initMocks(this);
}
@Test
public void test_Query_01() {
String msg = "{\n" +
" \"mainCode\":\"XXX\"\n" +
"}";
AAA vo = JSON.parseObject(msg,new TypeReference<AAA>(){
});
PowerMockito.when(service.query(Mockito.any())).thenReturn(new HashMap<>());
controller.query(vo);
}
错误:
Exception in thread "main" java.lang.NoSuchMethodError: org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;
at org.powermock.api.mockito.mockmaker.MockMakerLoader.doLoad(MockMakerLoader.java:45)
at org.powermock.api.mockito.mockmaker.MockMakerLoader.load(MockMakerLoader.java:36)
at org.powermock.api.mockito.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:36)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
03 分析依赖
1、打开pom文件
2、选择dependency analyzer,输入mock
同时我们了解到的是对于 PowerMock 2.x,您至少需要 Mockito 2.8.9+。
因此有兼容问题。
在给你们看一下正常的依赖关系是这样的:
因此我们就要手动修改一下它的pom了,如下:
1、右键点击exclude
2、添加兼容的mockito版本
最后的效果:文章来源:https://www.toymoban.com/news/detail-402881.html
<!-- PowerMock+Mockito+Junit4.4 -Above-->
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.0-RC.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.0-beta.5</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.9</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>2.15.0</version>
<exclusions>
<exclusion>
<artifactId>mockito-core</artifactId>
<groupId>org.mockito</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.23.4</version>
</dependency>
03 问题分析
一句话,我这个项目的springBoot版本比较低,连带着mockito的版本也较低,产生了兼容的问题。文章来源地址https://www.toymoban.com/news/detail-402881.html
到了这里,关于单元测试(mock) 错误org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;解析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!