Use instance factory for value classes with any() matcher #1403
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #1199: "registerInstanceFactory supplier is not used for value classes"
When using
registerInstanceFactory
for a value class that has aninit{}
block for validation, the factory was not being invoked when the value class was used as an argument with theany()
matcher. This would lead to MockK attempting to create a default instance of the value class, potentially violating theinit
block's requirements and causing anIllegalArgumentException
.This commit addresses the issue by modifying
JvmSignatureValueGenerator.signatureValue
. Now, when a signature value is needed for a value class (e.g., for anany()
matcher), the code first attempts to obtain an instance viainstantiateViaInstanceFactoryRegistry
. 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.