Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 1, 2025

Random.nextLong(long, long) was introduced in Java 17 via RandomGenerator interface, breaking compatibility for projects targeting Java 8/11. This caused NoSuchMethodError when using any() matchers with Long parameters.

java.lang.NoSuchMethodError: 'long java.util.Random.nextLong(long, long)'
    at io.mockk.impl.recording.JvmSignatureValueGenerator.generateSafeLong(JvmSignatureValueGenerator.kt:74)

Changes

  • Replace rnd.nextLong(origin, bound) calls with custom nextLongInRange(origin, bound) method
  • Use Math.floorMod(rnd.nextLong(), range) for correct bounded generation with negative values
  • Add input validation via require check

Fixes #1433

Original prompt

This section details on the original issue you should resolve

<issue_title>Mockk 1.14.6 is no longer compatible with Java 11</issue_title>
<issue_description>Hi,
I have encountered issue with failing test when updating to the latest bugfix version of Mockk 1.14.5 -> 1.14.6

I have noticed that the PR #1424 has introduced a new method

    private fun generateSafeLong(): Long {
        return if (rnd.nextBoolean()) {
            // Generate positive value in safe millisecond range
            rnd.nextLong(MAX_NANOS_IN_MILLIS + 1, MAX_MILLIS)
        } else {
            // Generate negative value in safe millisecond range
            rnd.nextLong(-MAX_MILLIS, -MAX_NANOS_IN_MILLIS)
        }
    }

This method calls method from RandomGenerator.nextLong(long, long) interface which is no present for Random instance in Project which targets Java 11, which results in folowing error

java.lang.NoSuchMethodError: 'long java.util.Random.nextLong(long, long)'
	at io.mockk.impl.recording.JvmSignatureValueGenerator.generateSafeLong(JvmSignatureValueGenerator.kt:74)
	at io.mockk.impl.recording.JvmSignatureValueGenerator.instantiate(JvmSignatureValueGenerator.kt:48)
	at io.mockk.impl.recording.JvmSignatureValueGenerator.signatureValue(JvmSignatureValueGenerator.kt:35)
	at io.mockk.impl.recording.states.RecordingState.matcher(RecordingState.kt:46)
	at io.mockk.impl.recording.CommonCallRecorder.matcher(CommonCallRecorder.kt:52)
	at my.example.Test.create$lambda$5$lambda$2(Test.kt:221)
	at io.mockk.impl.eval.RecordedBlockEvaluator$record$block$1.invoke(RecordedBlockEvaluator.kt:24)
	at io.mockk.impl.eval.RecordedBlockEvaluator$enhanceWithRethrow$1.invoke(RecordedBlockEvaluator.kt:76)

Build environment

Daemon JVM: JetBrains JDK 21.0.8+-14018985-b1038.68
  | Location:           *****
  | Language Version:   21
  | Vendor:             JetBrains
  | Architecture:       aarch64
  | Is JDK:             true

Kotlin version: 2.2.10

Example usage:

        val builder: OkHttpClient.Builder = mockk {
            every { connectTimeout(any(), any()) } returns this
        }
    fun connectTimeout(
      timeout: Long,
      unit: TimeUnit,
    )
  //...

Workarounds:

  • Update Project Java version to 17 (not feasible)
  • Don't use any() matchers for Long

Requests:

  • Specify supported Java version by the Mockk
  • Don't break Java compatibility in bugfix version
    </issue_description>

<agent_instructions>seems some breaking change affects downstream projects. please fix it</agent_instructions>

Comments on the Issue (you are @copilot in this section)

Custom agent used: tech-lead
Tech lead expert for MockK - Kotlin's premier mocking library. Specializes in complex bug fixing, bytecode manipulation, DSL design, and multi-platform architecture. Proactively handles recursion issues, signature matching problems, and JVM agent complexities.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI self-assigned this Dec 1, 2025
@oleksiyp oleksiyp marked this pull request as ready for review December 1, 2025 11:55
Copilot AI changed the title [WIP] Fix Mockk 1.14.6 compatibility issue with Java 11 Fix Java 11 compatibility: replace Random.nextLong(long, long) with Java 8 compatible alternative Dec 1, 2025
Copilot AI requested a review from oleksiyp December 1, 2025 11:57
@Raibaz Raibaz merged commit ea99f88 into master Dec 2, 2025
26 of 45 checks passed
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

Successfully merging this pull request may close these issues.

Mockk 1.14.6 is no longer compatible with Java 11

3 participants