IntelliJ IDEA 2022.2.4 errors when running Mockito tests

已回答

Since upgrading my IDEA, I can no longer run my Mockito tests in the UI... this is the error I'm getting...

org.mockito.exceptions.base.MockitoException: 
Mockito cannot mock this class: interface java.sql.Connection.

If you're not sure why you're getting this error, please report to the mailing list.


Java               : 17
JVM vendor name    : JetBrains s.r.o.
JVM vendor version : 17.0.5+7-b469.71
JVM name           : OpenJDK 64-Bit Server VM
JVM version        : 17.0.5+7-b469.71
JVM info           : mixed mode
OS name            : Mac OS X
OS version         : 12.6

When I run the tests using Maven, they work perfectly fine. They only fail in IDEA.

My project uses Java 11, and I have Java 11 installed on my computer. But it appears that IDEA is running my tests using Java 17, even though I've set EVERY possible setting for my java compiler version and language level to 11.

  1. File | Project Structure | Project | SDK & Language level.
  2. File | Project Structure | Modules | <each module here> | Dependencies | Module SDK
  3. Settings/Preference | Build, Execution, Deployment | Compiler | Java Compiler.

I'm using mockito-kotlin and these are my dependencies:

[INFO] +- com.nhaarman.mockitokotlin2:mockito-kotlin:jar:2.2.0:test
[INFO] | \- org.mockito:mockito-core:jar:4.0.0:test
[INFO] | \- org.objenesis:objenesis:jar:3.2:test
[INFO] +- org.mockito:mockito-junit-jupiter:jar:4.0.0:test
[INFO] | \- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
[INFO] | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test
[INFO] | \- org.apiguardian:apiguardian-api:jar:1.1.2:test

 

 

The only information I find on Google is 4+ years old and not specific to IDEA (the tests are also failing with gradle or maven).

The previous version of IDEA (2022.2) also was having this issue (I was hoping the update would fix this, it did not). However, version 2021.2 is not having this issue, it runs my tests just fine. I have it installed on an old computer, and I'm running the exact same code - checked out from git-hub.

I believe the underlying cause is that IDEA 2022.2.* is using Java 17 to run the tests, but my code is being compiled with Java 11. How can I get IDEA to use Java 11 to run my tests?

TIA

Tonya

0

Hi, @Tohrel

Java               : 17
JVM vendor name    : JetBrains s.r.o.
JVM vendor version : 17.0.5+7-b469.71

 

This is the JDK for running the IDEA itself though it is odd here that IDEA tried to use this JDK to build/run your project.

IDEA 2021.x bundles JDK 11 for running the IDEA so I guess that is the reason why you don't have this problem in that version.

Please click the IDE main menu `Run | Edit Configuration` and check what JDK you use there. Is it the JDK 11?

If it doesn't help, please share a screenshot of the full IDEA window with this "org.mockito.exceptions.base.MockitoException" error and attach the full output text of the errors (you could ctrl+a and copy it and upload it here https://uploads.jetbrains.com/ and share the upload id here).

If this error is from the Run tool window when running, the first line would contain the full command to run this program and this might give us some clues.


0

This setting you have directed me to already indicates java 11, as seen in the screenshot below.

I am extremely swamped at work, so I will try to get some small project sent to you, but I don't know if I can get to it this week. Currently I am going to have to downgrade me IDEA to a previous version just to get my work done, and put this issue on the back-burner. I reported this bug months ago and had to put it on the back-burner then as well, and ultimately my bug just got closed. Hopefully that doesn't happen again, I will at least keep in contact.

The project I'm running is written in Kotlin, and is using spring boot, and Maven.

Thank you,
Tonya

0
Sure, feel free to send us the sample project when you have time later.

If you have no time to get a reproduce project, you could share a screenshot of the full IDEA window with this "org.mockito.exceptions.base.MockitoException" error and attach the full output text of the errors (you could ctrl+a and copy it and upload it here https://uploads.jetbrains.com/ and share the upload id here).
So we could see any clues there.

> I reported this bug months ago and had to put it on the back-burner then as well, and ultimately my bug just got closed.

Do you report this issue via a ticket or via a bug report at https://youtrack.jetbrains.com/issues/IDEA?u= ?
If you report it in a ticket, and if there is no reply from your side after we ask for more details, our system will close it automatically after two weeks. But if you reply to that ticket again, our system will reopen it and we could continue to help you there.
0

I had this same issue after updating from 2022.something to 2023.1. It turns out that my "11" configuration was actually pointing to the internal IntelliJDK, which was JDK 11 before the update, but now it's JDK 17. I pointed "11" to my AdoptOpenJDK 11 installation and everything worked again.

0

请先登录再写评论。