单元测试(mock) 错误org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;解析

Posted 栗子~~

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单元测试(mock) 错误org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;解析相关的知识,希望对你有一定的参考价值。

文章目录

前言

  如果您觉得有用的话,记得给博主点个赞,评论,收藏一键三连啊,写作不易啊^ _ ^。
  而且听说点赞的人每天的运气都不会太差,实在白嫖的话,那欢迎常来啊!!!


单元测试(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版本
最后的效果:

       <!-- 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的版本也较低,产生了兼容的问题。

以上是关于单元测试(mock) 错误org.mockito.Mockito.framework()Lorg/mockito/MockitoFramework;解析的主要内容,如果未能解决你的问题,请参考以下文章

Junit mockito

单元测试 - mock异常

基于Junit5+PowerMock的单元测试框架

出现错误 java.lang.NoClassDefFoundError: org/mockito/MockitoAnnotations$Mock

Mock Final方法和类

Mock Final方法和类