Skip to content

Conversation

@LarsSven
Copy link
Contributor

@LarsSven LarsSven commented Jul 1, 2024

This MR serves to add support for value classes to Mockito Kotlin. Value classes currently break and there is no easy way around them, many users have had issues with this: #445

This MR adds an anyValueClass() and adds it to the any() and anyOrNull() implementation. I expect I may have done a few things non-optimally as I am not too familiar with Mockito's inner workings, but I look forward to feedback from someone who knows more about Mockito argument matchers than me to help me improve this. I do think it is important to get support for value classes.

@TimvdLippe
Copy link
Contributor

Not sure why, but tests are failing on this PR. Can you please take a look and ensure all tests are passing?

@LarsSven
Copy link
Contributor Author

LarsSven commented Jul 5, 2024

I resolved the issues with the tests, it was a relatively obscure issue that had to do with verify not being called for the test where we expected it to error out. I also went ahead and added more tests to test other scenarios like around null vs non-null and nested value classes.

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the fix!

@TimvdLippe TimvdLippe merged commit 888e19d into mockito:main Jul 9, 2024
@vans239
Copy link

vans239 commented Apr 10, 2025

Is there any plan to add support for eq function?

We use snippet below currently, so it should work for eq also

inline fun <reified T> eqValue(v: T): T {
    val field = T::class.java.declaredFields.first { !Modifier.isStatic(it.modifiers) }.apply { isAccessible = true }
    val method = T::class.java.getDeclaredMethod("box-impl", field.type).apply { isAccessible = true }
    return method.invoke(null, ArgumentMatchers.eq(field.get(v))) as T
}

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.

3 participants