Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Mockito cannot create mocks after the project moves to a newer JDK

Your error

org.mockito.exceptions.base.MockitoException: Could not modify all classes2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The Byte Buddy that Mockito brings is older than your JDK

Declare both Byte Buddy artifacts yourself in test scope at the newest release.

Fix
<!-- pom.xml -->
<dependency>
  <groupId>net.bytebuddy</groupId>
  <artifactId>byte-buddy</artifactId>
  <version>1.18.17</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>net.bytebuddy</groupId>
  <artifactId>byte-buddy-agent</artifactId>
  <version>1.18.17</version>
  <scope>test</scope>
</dependency>

// build.gradle
testImplementation 'net.bytebuddy:byte-buddy:1.18.17'
testImplementation 'net.bytebuddy:byte-buddy-agent:1.18.17'
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

MockitoException: Could not modify all classes means Byte Buddy, the library Mockito uses to rewrite classes, is older than the JDK running your tests; the last Caused by line says Java N is not supported. Put byte-buddy and byte-buddy-agent 1.18.17 on the test classpath, or set byte-buddy.version under Spring Boot. Mockito 5.24.0 still declares Byte Buddy 1.17.7.

  • Applies to Mockito 5.24.0, Byte Buddy 1.18.17, Spring Boot 4.2.0, Gradle 9.9
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Scroll to the deepest Caused by line and note the Java version it names. Then list which Byte Buddy release your test classpath really resolves and which dependency brought it in. Those two numbers decide the cause.

Run this first
# Maven
mvn dependency:tree -Dincludes=net.bytebuddy
# Gradle
./gradlew -q dependencyInsight --dependency byte-buddy --configuration testRuntimeClasspath

Then find the line you see in this list and open its fix:

  1. 1The Byte Buddy that Mockito brings is older than your JDKThe tree shows byte-buddy arriving only through mockito-core, at 1.17.7 or lower, and no Spring Boot BOM is involved
  2. 2Spring Boot's managed version keeps an older Byte Buddy in placeThe project inherits spring-boot-starter-parent or imports the Boot BOM, and the tree still shows the old byte-buddy even after you raised mockito.version
  3. 3The JDK is newer than every Byte Buddy release so farThe tree already shows the newest Byte Buddy, 1.18.17, and the deepest Caused by line still reports an unsupported Java version

What does the error look like?

  • org.mockito.exceptions.base.MockitoException: Could not modify all classes
  • Caused by: java.lang.IllegalStateException: Byte Buddy could not instrument all classes within the mock's type hierarchy
  • Caused by: java.lang.IllegalArgumentException: Java 21 (65) is not supported by the current version of Byte Buddy which officially supports Java 20 (64) - update Byte Buddy or set net.bytebuddy.experimental as a VM property

Cause 1 of 3

The Byte Buddy that Mockito brings is older than your JDK

You seeThe tree shows byte-buddy arriving only through mockito-core, at 1.17.7 or lower, and no Spring Boot BOM is involved

Check

Could not modify all classes is only Mockito's wrapper. Underneath, Byte Buddy refuses to read class files from a Java release it does not know yet. The mockito-core 5.24.0 POM still lists byte-buddy 1.17.7, while Byte Buddy's own release notes add ASM support for Java 26 in 1.17.8 and for Java 28 in 1.18.15. The scalac and Android hint in the middle exception is a red herring here.

Check
mvn dependency:tree -Dincludes=net.bytebuddy:byte-buddy

Fix

Declare both Byte Buddy artifacts yourself in test scope at the newest release. Maven uses the nearest definition, so your direct entry beats the one Mockito pulls in; Gradle's conflict resolution keeps the higher version. Keep byte-buddy and byte-buddy-agent on the same number.

Fix
<!-- pom.xml -->
<dependency>
  <groupId>net.bytebuddy</groupId>
  <artifactId>byte-buddy</artifactId>
  <version>1.18.17</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>net.bytebuddy</groupId>
  <artifactId>byte-buddy-agent</artifactId>
  <version>1.18.17</version>
  <scope>test</scope>
</dependency>

// build.gradle
testImplementation 'net.bytebuddy:byte-buddy:1.18.17'
testImplementation 'net.bytebuddy:byte-buddy-agent:1.18.17'

Verify

Running the tree again shows 1.18.17 for both artifacts, and the test that built the mock passes.

Verify
mvn test

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

Spring Boot's managed version keeps an older Byte Buddy in place

You seeThe project inherits spring-boot-starter-parent or imports the Boot BOM, and the tree still shows the old byte-buddy even after you raised mockito.version

Check

Spring Boot controls Byte Buddy through its own byte-buddy.version property, apart from mockito.version. In Maven, a version set in dependency management wins over mediation for transitive artifacts, so bumping Mockito alone changes nothing for Byte Buddy. Boot 4.2.0 manages byte-buddy 1.18.17, while Boot 3.5.12 still manages 1.17.8.

Check
grep -nE "spring-boot-starter-parent|spring-boot-dependencies|io.spring.dependency-management" pom.xml build.gradle

Fix

The clean route is a Boot upgrade, which moves Byte Buddy and Mockito together. If you cannot upgrade yet, override the property. Boot's docs warn that a changed managed version may cause compatibility issues, so drop the override at your next Boot upgrade.

Fix
<!-- pom.xml, with spring-boot-starter-parent -->
<properties>
  <byte-buddy.version>1.18.17</byte-buddy.version>
</properties>

// build.gradle, with io.spring.dependency-management
ext['byte-buddy.version'] = '1.18.17'

Verify

The dependency tree lists byte-buddy 1.18.17, and the Caused by line about the unsupported Java version no longer appears.

Verify
./gradlew test

Confirmed by docs.spring.io

Did this fix it?

Cause 3 of 3

The JDK is newer than every Byte Buddy release so far

You seeThe tree already shows the newest Byte Buddy, 1.18.17, and the deepest Caused by line still reports an unsupported Java version

Check

Byte Buddy compares each class file with the newest Java version it knows and throws unless the net.bytebuddy.experimental system property is true. With the property set, it reads the newer class file as if it were the latest known version. That is meant for early-access JDKs, not for long-term use.

Check
./gradlew -q dependencyInsight --dependency byte-buddy-agent --configuration testRuntimeClasspath

Fix

Pass the property to the forked test JVM only, and delete it as soon as a Byte Buddy release names your JDK.

Fix
<!-- pom.xml -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <configuration>
    <argLine>-Dnet.bytebuddy.experimental=true</argLine>
  </configuration>
</plugin>

// build.gradle
test {
    systemProperty 'net.bytebuddy.experimental', 'true'
}

Verify

Mocks are created and the suite runs. Leave a comment next to the setting so it is removed after the next Byte Buddy upgrade.

Verify
mvn test

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • When you raise the JDK in the toolchain or Docker image, check Byte Buddy's release notes for that Java version in the same pull request.
  • Run the test suite in CI on the newest JDK you plan to adopt, so the class file check fails there first.
  • Under Spring Boot, change byte-buddy.version and mockito.version only together with a Boot upgrade, never one of them alone.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: QistLock, Dispatch Pro, Nuclear Home Video.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.