Skip to content

Mockito "cannot mock this class" error when running multiple tests in single JVM #27

@shermosa

Description

@shermosa

The problem

When running tests individually or in Eclipse, they all succeed. When run all together in Maven, Mockito throws this exception: Mockito cannot mock this class: class ___. Mockito can only mock non-private & non-final classes even if the class is non-private (protected), non-final and non-static.

How to reproduce it

1.- Delete this from the pom.xml file

<forkMode>always</forkMode>

2.- Run mvn package in the command line

Stacktrace

[ERROR] com.hotels.mutantswarm.report.ReportTest.testingReport  Time elapsed: 0.014 s  <<< ERROR! 
org.mockito.exceptions.base.MockitoException:  

Mockito cannot mock this class: class com.hotels.mutantswarm.report.Line. 

Mockito can only mock non-private & non-final classes. 
If you're not sure why you're getting this error, please report to the mailing list. 
 

Java              : 1.8 
JVM vendor name    : AdoptOpenJDK 
JVM vendor version : 25.265-b01 
JVM name           : OpenJDK 64-Bit Server VM 
JVM version        : 1.8.0_265-b01 
JVM info           : mixed mode 
OS name            : Mac OS X 
OS version         : 10.15.7


Underlying exception : org.mockito.exceptions.base.MockitoException:  
Cannot create mock for class com.hotels.mutantswarm.report.Line 

The type is not public and its mock class is loaded by a different class loader. 
This can have multiple reasons: 
 - You are mocking a class with additional interfaces of another class loader 
 - Mockito is loaded by a different class loader than the mocked type (e.g. with OSGi) 
 - The thread's context class loader is different than the mock's class loader 
Caused by: org.mockito.exceptions.base.MockitoException:  

Cannot create mock for class com.hotels.mutantswarm.report.Line 

The type is not public and its mock class is loaded by a different class loader. 
This can have multiple reasons: 
 - You are mocking a class with additional interfaces of another class loader 
 - Mockito is loaded by a different class loader than the mocked type (e.g. with OSGi) 
 - The thread's context class loader is different than the mock's class loader 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions