hello guys, I try to mock/verify String.format(......
# mockk
a
hello guys, I try to mock/verify String.format(....)
I know this is a inline function
So I expect this is replace at compile time
so how handle it
o
No, no way to mockk inlines
How would you do that test so ?
with a intermediate object that map the inline function ?
I even try to mock the inside of the inline function, but I got some issue...
o
ehh... I don't know if I should write this at all. There is no normal way to do that. Was just curious how to hack it.
Copy code
mockkConstructor(Formatter::class)

    every { anyConstructed<Formatter>().format("abc %s %d", "def", 123) } answers {
        mockk { every { this@mockk.toString() } returns "second" }
    }

    println("abc %s %d".format("def", 123))
But mocking other cases to return normal formatting is not easy:
Copy code
mockkConstructor(Formatter::class)

    every { anyConstructed<Formatter>().format(any(), *anyVararg()) } answers {
        val res = callOriginal()

        val toS = res::class.memberProperties.first {
            it.name == "a"
        }!!.getter.apply { isAccessible = true }(res)?.toString() ?: "null"

        mockk { every { this@mockk.toString() } returns toS }
    }
    every { anyConstructed<Formatter>().format("abc %s %d", "def", 123) } answers {
        mockk { every { this@mockk.toString() } returns "second" }
    }

    println("abc  %s".format("def"))
    println("abc %s %d".format("ghi", 123))
    println("abc %s %d".format("def", 123))

`
l
Just pass a string that wil be formatted
And verify that the end value is correct. You're mocking too much of the implementation, and that's usually a bad practice
1
t
Agree! I come from the day of jmock, I mock only interfaces (in general). Static methods don't define a contract, they are specific implementation I see no reason (well I do, but usually I see smell like missing providers or clean code practice) to mock them as the behavior is one and only one.
a
Thanks for your comment, there are valuables. The point is that I’m on Android, so I need to resolve my string resources to run what you said. (I’m also trying to run roboelectric to get this working, but I have an open issue). So my idea was to spy the “format” function to then verify the sequence call of this “format” function with expected param. Would you do in an other way @oleksiyp @LeoColman @thanksforallthefish ?
o
Extract format to a class with interface
And mock it
Ah... probably it is because you need to deal with resources
Do you need this test at all? 😄 what's its value?
a
It is contractual...
sometimes it is more easy/faster/safer to test everything, than ask is you should test or not !
would it be complexe to be able to spyk it ? as well as spy static and object (that are not available in mockk today)
o
mockkStatic and mockkObject are rather spy than mock
a
... but when you use mockkStatic and mockkObject you have to define every stub, isn’t it ? so from my understanding I would not call it a spy. Am I wrong ?
o
You don't need to define
a
if I mock Application.Companion.applicationContext, then I have to setup the return value like :
Copy code
mockkObject(EDFApplication.Companion)

every { Application.applicationContext().getString(any()) } returns ""
every { Application.applicationContext().getString(any(), any(), any()) } returns ""
144 Views