Skip to content

Conversation

esafak
Copy link

@esafak esafak commented Jun 13, 2025

Fixes #1199: "registerInstanceFactory supplier is not used for value classes"

When using registerInstanceFactory for a value class that has an init{} block for validation, the factory was not being invoked when the value class was used as an argument with the any() matcher. This would lead to MockK attempting to create a default instance of the value class, potentially violating the init block's requirements and causing an IllegalArgumentException.

This commit addresses the issue by modifying JvmSignatureValueGenerator.signatureValue. Now, when a signature value is needed for a value class (e.g., for an any() matcher), the code first attempts to obtain an instance via instantiateViaInstanceFactoryRegistry. If a factory is registered for the value class, it will be used. Otherwise, MockK falls back to the previous behavior of creating an instance from the boxed underlying type.

A new test case was added to ValueClassTest.kt to specifically reproduce this bug and verify the fix. This test, along with other relevant tests, now passes.

This PR was computer generated, then refactored by me.

Fixes mockk#1199: "registerInstanceFactory supplier is not used for value classes"

When using `registerInstanceFactory` for a value class that has an `init{}` block for validation, the factory was not being invoked when the value class was used as an argument with the `any()` matcher. This would lead to MockK attempting to create a default instance of the value class, potentially violating the `init` block's requirements and causing an `IllegalArgumentException`.

This commit addresses the issue by modifying `JvmSignatureValueGenerator.signatureValue`. Now, when a signature value is needed for a value class (e.g., for an `any()` matcher), the code first attempts to obtain an instance via `instantiateViaInstanceFactoryRegistry`. If a factory is registered for the value class, it will be used. Otherwise, MockK falls back to the previous behavior of creating an instance from the boxed underlying type.

A new test case was added to `ValueClassTest.kt` to specifically reproduce this bug and verify the fix. This test, along with other relevant tests, now passes.
@Raibaz Raibaz merged commit c3aa4db into mockk:master Jun 15, 2025
22 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.

registerInstanceFactory supplier is not used for value classes
2 participants