mockk

mockk verify lambda argument

∥☆過路亽.° 提交于 2019-12-10 14:23:16
问题 I'd like to verify the value which was passed in via a lamdba. The func looks like this: fun save(entity: Any, idSupplier: () -> UUID): JsonEntity { return save(JsonEntity(idSupplier(), entity, entity::class.simpleName!!)) } Now within my test I'd like to verify the value which has been passed in for the idSupplier. I made a mock to return a value for the save(...) which is called in my own save(..., () -> ...) like this every { jsonStorage.save(any<JsonEntity>()) } answers { value } Now on

How do you Mockk a Kotlin top level function?

泪湿孤枕 提交于 2019-12-08 15:58:58
问题 Mockk allows mocking static functions, but how does one mock a Kotlin top level function? For example, if I have a Kotlin file called HelloWorld.kt , how do I mock the sayHello() function? HelloWorld.kt fun sayHello() = "Hello Kotlin!" 回答1: There is way to mockk a top level function: mockkStatic("pkg.FileKt") every { fun() } returns 5 You just need to know which file this function goes. Check in JAR or stack trace. 回答2: Building on @Sergey's answer: You could have the actual implementation of

Mock static java methods using Mockk

主宰稳场 提交于 2019-12-05 14:54:23
问题 We are currently working with java with kotlin project, slowly migrating the whole code to the latter. Is it possible to mock static methods like Uri.parse() using Mockk? How would the sample code look like? 回答1: MockK allows mocking static Java methods. The main purpose for it is mocking of Kotlin extension functions, so it is not as powerful as PowerMock, but still does it's job even for Java static methods. The syntax would be following: staticMockk<Uri>().use { every { Uri.parse("http:/