Add first-class support for mocking object singletons#587
Add first-class support for mocking object singletons#587jselbo merged 2 commits intomockito:mainfrom
Conversation
|
|
||
| // This will compile to a static method callable by both Java and Kotlin. | ||
| // It must be mocked and stubbed with mockStatic! | ||
| @JvmStatic fun helloStatic(): String = "static_unstubbed" |
There was a problem hiding this comment.
This is the only gotcha - @JvmStatic marked methods in object (but not companion object) compile only to a static method and must be mocked with mockStatic.
I plan to update the wiki page with more info about mocking objects after this is merged!
There was a problem hiding this comment.
Dang, this is super tricky!
I wonder if we could signal this somehow to people.
I know this is also what libs like MockK do, but if we could surface that somehow, then it could help Mockito stand out. That'd be a whole new discussion though, outside this PR.
My random idea
What if Mockito had a lint lib that people could easily pull and use?
There was a problem hiding this comment.
Lint is an interesting idea, though it would be hard to get traction. I'd prefer a built-in solution.
I also hate that this gotcha exists. We could try to be smart and invoke mockStatic under the hood if we detect @JvmStatic methods. Let me explore this option.
There was a problem hiding this comment.
Wow! I LOVED your solution! Really clever, ship it! 🚀
| implementation "org.jetbrains.kotlin:kotlin-reflect:2.1.20" | ||
|
|
||
| api "org.mockito:mockito-core:5.20.0" | ||
| api "org.mockito:mockito-core:5.23.0" |
There was a problem hiding this comment.
Oh, wow! I'm surprised this is not extracted to a param somewhere so we'd have a single source of truth for the version across the mockito libs.
Interesting to know, but certainly not something related to this PR.
There was a problem hiding this comment.
This is the only place we specify the mockito-core version in this repo. But there duplicated versions across the build.gradle files. As I follow-up I can convert this to use Gradle version catalog
There was a problem hiding this comment.
This is a bit naturally grown and should certainly be fixed.
|
|
||
| // This will compile to a static method callable by both Java and Kotlin. | ||
| // It must be mocked and stubbed with mockStatic! | ||
| @JvmStatic fun helloStatic(): String = "static_unstubbed" |
There was a problem hiding this comment.
Dang, this is super tricky!
I wonder if we could signal this somehow to people.
I know this is also what libs like MockK do, but if we could surface that somehow, then it could help Mockito stand out. That'd be a whole new discussion though, outside this PR.
My random idea
What if Mockito had a lint lib that people could easily pull and use?
…nd wrapping the closeables in a MockedObject type
|
|
||
| // This will compile to a static method callable by both Java and Kotlin. | ||
| // It must be mocked and stubbed with mockStatic! | ||
| @JvmStatic fun helloStatic(): String = "static_unstubbed" |
There was a problem hiding this comment.
Wow! I LOVED your solution! Really clever, ship it! 🚀
raphw
left a comment
There was a problem hiding this comment.
Looks good to me, too. Clean and well-documented.
Fixes: #536
This builds off the new
mockSingletonfeature of Mockito-core which enables thread-local intercepting instance methods of existing objects. This was added to enable mocking Kotlinobjectandcompanion objectsingletons. See mockito/mockito#3762Very excited to finally add this feature!
This is comparable to MockK's
mockkObject.