I have a simple Kotlin MockK test to mock a static method in setup(). However, instead of mocking, the underlying static method is just called? How do you properly mock static methods in Kotlin?
class Test {
@Before
fun setup() {
mockkStatic(StaticClass::class)
// mockkStatic(StaticClass::testStaticMethod)
// mockkObject(StaticClass)
// This calls the static method instead of mocking???
every { StaticClass.testStaticMethod() } returns Unit
}
...
class StaticClass {
companion object {
// JvmStatic
fun testStaticMethod() {
print("why is this running still??") <== ???
coevery
as well.setup
or test
methods.io.mockk:mockk
.mockk
no junit or other testing frameworks.The code in your example wouldn't run for me with Missing mocked calls inside every { ... } block
error. However, when setup like described here it runs and print
inside testStaticMethod
doesn't happen. I'm using JUnit5 here instead of JUnit4, but I don't think it matters.
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class Test {
@BeforeAll
fun setup() {
mockkObject(StaticClass)
every { testStaticMethod() } just Runs
}
@AfterAll
fun tearDown() {
unmockkObject(StaticClass)
}
@Test
fun mainTest() {
testStaticMethod()
}
}