Mockito cannot mock this class

We Are Going To Discuss About Mockito cannot mock this class. So lets Start this Java Article.

Mockito cannot mock this class

  1. Mockito cannot mock this class

    It is because of Java/Mockito compatibility. For example, if you are using Java 11 with Mockito 2.17.0, it will throw the same error (Upgrading to Mockito 2.22.0 or later will solve it). Aside from digging into their release artifact on Github, I am yet to see a comprehensive document that spells out the compatibility matrix. Here is a helpful compatibility Matrix (based on quick test)

  2. Mockito cannot mock this class

    It is because of Java/Mockito compatibility. For example, if you are using Java 11 with Mockito 2.17.0, it will throw the same error (Upgrading to Mockito 2.22.0 or later will solve it). Aside from digging into their release artifact on Github, I am yet to see a comprehensive document that spells out the compatibility matrix. Here is a helpful compatibility Matrix (based on quick test)

Solution 1

It is because of Java/Mockito compatibility. For example, if you are using Java 11 with Mockito 2.17.0, it will throw the same error (Upgrading to Mockito 2.22.0 or later will solve it). Aside from digging into their release artifact on Github, I am yet to see a comprehensive document that spells out the compatibility matrix. Here is a helpful compatibility Matrix (based on quick test)

  • Mockito * – 2.17.0 => Java 8

(footnote: * – dont know how far back)

  • Mockito 2.18.0 – 3.2.4 => Java 11

Original Author papigee Of This Content

Solution 2

Even though it’s not specifically answering the question (question is Java 8), I came here via Google. Using Java 11, I solved this issue by including the following dependency in build.gradle:

compile group: 'org.mockito', name: 'mockito-core', version: '2.22.0'

as discussed here

Original Author JoschJava Of This Content

Solution 3

I had the same issue.

my jdk version is 11.0.4+10-LTS.

I use org.springframework.boot:spring-boot-starter-parent:2.0.7.RELEASE

I upgrade Mockito to 3.3.0

The exception still happen.

Then I did this:

  1. exclusion mockito-core from spring-boot-starter-test

  2. add below to pom file

       <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>3.3.0</version>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>net.bytebuddy</groupId>
                    <artifactId>byte-buddy</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>net.bytebuddy</groupId>
                    <artifactId>byte-buddy-agent</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy</artifactId>
            <version>1.10.5</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>net.bytebuddy</groupId>
            <artifactId>byte-buddy-agent</artifactId>
            <version>1.10.5</version>
            <scope>test</scope>
        </dependency>

Original Author wuxinshui Of This Content

Solution 4

When I examined my Java-8 project’s dependency tree I found that spring-boot-starter-test was using Mockito 2.15.0. Fixed by excluding that transitive Mockito dependency and re-adding it explicitly with version 2.23.4 (and re-build if not done automatically). Snippet of my maven POM:

<dependencies>
...
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>2.23.4</version>
</dependency>
</dependencies>

If using gradle it will look something like:

dependencies {
    ...
    def withoutMockito = {
        exclude group: 'org.mockito', module: 'mockito-core'
    }
    testImplementation "org.springframework.boot:spring-boot-starter-test:2.0.2-RELEASE", withoutMockito
    testImplementation "org.mockito:mockito-core:2.23.4"
 }

Original Author Adrian Redgers Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

Siddharth

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment