Anthony f
03/07/2019, 5:28 PMoleksiyp
03/07/2019, 5:34 PMAnthony f
03/07/2019, 5:38 PMoleksiyp
03/07/2019, 7:18 PMmockkConstructor(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:
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))
`
LeoColman
03/07/2019, 10:11 PMthanksforallthefish
03/08/2019, 6:37 AMAnthony f
03/14/2019, 10:45 AMoleksiyp
03/14/2019, 10:59 AMAnthony f
03/14/2019, 11:11 AMoleksiyp
03/14/2019, 11:16 AMAnthony f
03/14/2019, 11:22 AMoleksiyp
03/14/2019, 11:23 AMAnthony f
03/14/2019, 11:27 AMmockkObject(EDFApplication.Companion)
every { Application.applicationContext().getString(any()) } returns ""
every { Application.applicationContext().getString(any(), any(), any()) } returns ""