Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shermosa opened this issue Feb 1, 2021 · 0 comments

Comments

@shermosa
Copy link
Contributor

shermosa commented Feb 1, 2021

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 
@massdosage massdosage changed the title Mockito bug Mockito "cannot mock this class" error when running multiple tests in single JVM Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant