Hello folks, I am trying mocking for the first tim...
# mockk
n
Hello folks, I am trying mocking for the first time using MockK library. But apparently I am doing something wrong and my IDE is complaining about it. I am not able to figure out where is the problem. Here is the code I have written,
Copy code
package mock

import io.mockk.mockk

internal class DummyDatabaseTest {
    val mockConnection: DummyJdbiConnection = mockk()

    every { mockConnection.status() } returns "This is JDBI conncetion"
}
Here is my
build.gradle
file,
Copy code
dependencies {
    implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.31'

    testImplementation 'org.junit.jupiter:junit-jupiter:5.8.1'
    testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.1'
    testImplementation 'io.kotest:kotest-assertions-core:4.6.3'
    testImplementation 'io.mockk:mockk:1.12.0'
}
Please see the attached screenshot to find out about the error I'm getting. Can anyone point out where is it going wrong? Do I have to do some manual imports?
e
You can't put statements in the class body
☝️ 1
n
What a naive mistake! I am surprised 😅 Btw, thanks for your help.
e
what you could do, btw.. is:
Copy code
val mockConnection: DummyJdbiConnection = mockk() {
  every { status() } returns "This is a JDBI connection"
}
The above will pass the mocking lambda to the
mockk()
function.
K 1
n
That's cool. Actually I am new to mocking and testing in general. I have started reading the "mocking is no rocket science" series from kt.academy. But I was confused what the
every {...} returns ...
line is actually doing. At first I thought the
mockk()
function would create an instance of that object for me. So I was confused. Now I got that. It's literally mocking a function's behaviour for the mock instance. This is amazing!