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

NoSuchElementException in verify() #1221

Open
mmimica opened this issue Feb 22, 2024 · 3 comments
Open

NoSuchElementException in verify() #1221

mmimica opened this issue Feb 22, 2024 · 3 comments

Comments

@mmimica
Copy link

mmimica commented Feb 22, 2024

I'm calling a verify(timeout = 3_000, exactly = 3) { mock.offer(any()) }
and I'm getting:

java.util.NoSuchElementException: List is empty.
	at kotlin.collections.CollectionsKt___CollectionsKt.first(_Collections.kt:214)
	at io.mockk.impl.verify.UnorderedCallVerifier.matchCall(UnorderedCallVerifier.kt:110)
	at io.mockk.impl.verify.UnorderedCallVerifier.verify(UnorderedCallVerifier.kt:40)
	at io.mockk.impl.verify.TimeoutVerifier.verify(TimeoutVerifier.kt:22)
	at io.mockk.impl.recording.states.VerifyingState$recordingDone$outcome$1.invoke(VerifyingState.kt:31)
	at io.mockk.impl.recording.states.VerifyingState$recordingDone$outcome$1.invoke(VerifyingState.kt:30)
	at io.mockk.impl.recording.CommonCallRecorder.safeExec(CommonCallRecorder.kt:72)
	at io.mockk.impl.recording.states.VerifyingState.recordingDone(VerifyingState.kt:30)
	at io.mockk.impl.recording.CommonCallRecorder.done(CommonCallRecorder.kt:47)
	at io.mockk.impl.eval.RecordedBlockEvaluator.record(RecordedBlockEvaluator.kt:63)
	at io.mockk.impl.eval.VerifyBlockEvaluator.verify(VerifyBlockEvaluator.kt:30)
	at io.mockk.MockKDsl.internalVerify(API.kt:120)
	at io.mockk.MockKKt.verify(MockK.kt:218)
	at io.mockk.MockKKt.verify$default(MockK.kt:209)

Method signature is @NotNull Resp offer(@NotNull Req req) and it is mocked with

val capture = CopyOnWriteArrayList<Req>()
mock.apply {
                    every { offer(capture(capture)) } answers {
                        ...
                        Resp()
                    }

Nothing special IMO. I can't reproduce it but hope the above helps.

  • MockK version: 1.13.9
  • OS: Linux
  • Kotlin version: 1.9.21
  • JDK version: 17
@Raibaz
Copy link
Collaborator

Raibaz commented Feb 23, 2024

Why are you using CopyOnWriteArrayList for argument capturing?

capture supports slot and MutableList, I don't think it works with CopyOnWriteArrayList.

@mmimica
Copy link
Author

mmimica commented Feb 23, 2024

CopyOnWriteArrayList is a MutableList.

@mmimica
Copy link
Author

mmimica commented Feb 23, 2024

And I use it because the mock is invoked from different threads concurrently. The capturing list must be thread-safe.

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

2 participants