How to mock objects in relaxed mode? - kotlin

I have an object
object Foo {
fun doSomething(param: String) {
throw Exception()
}
}
I want it to become a stub (relaxed mock in mockk terminology) in my test.
Other words, I want this test to pass without exception:
#Test
fun shouldAskFooWithCorrectParams() {
mockkObject(Foo) // How to change it to make Foo a stub
Foo.doSomething("hey!")
verify(exactly = 1) { Foo.doSomething("hey!") }
}

Additional every { Foo.doSomething(any()) } answers {} does the trick for a single method.
This test is passes:
#Test
fun shouldAskFooWithCorrectParams() {
mockkObject(Foo)
every { Foo.doSomething(any()) } answers {}
Foo.doSomething("hey!")
verify(exactly = 1) { Foo.doSomething("hey!") }
}

Related

Mockk unmockk() method is not destroying mocks

I am running the following test code, but in some weird way I am not understanding it is not clearing the mocks in between the two tests.
I am getting environmentAsRequired as a return of repo.getClusterEnvironment(environmentName) on the second test instead of it throwing NoResultException() which causes the second test to fail because the exception I am asserting does not get thrown.
I have already verified and the method annotated with #BeforeEach is being called in between tests.
Anyone has any idea?
class ConfigUtilsTest {
#BeforeEach
fun start() {
MockKAnnotations.init(this)
val testModule = module() {
single { mockk<Repository>() }
single { mockk<AdminClient>() }
}
startKoin { modules(testModule) }
}
#AfterEach
fun stop() {
stopKoin()
unmockkAll()
}
#Test
fun `fetchOrCreateCluster creates the cluster if it is not on db but all required attributes are`() {
val environmentName = "environmentForCluster"
val clusterName = "NameForCluster"
val clusterURL = "UrlForCluster"
val environmentAsRequired = ClusterEnvironment(0, environmentName)
val expectedResult = Cluster(
clusterURL,
clusterName,
environmentAsRequired,
)
val repo = get(Repository::class.java)
every { repo.getCluster(clusterURL) } throws NoResultException()
every { repo.getClusterEnvironment(environmentName) } returns environmentAsRequired
fetchOrCreateCluster(
clusterName,
environmentName,
clusterURL
) shouldBe expectedResult
}
#Test
fun `fetchOrCreateCluster throws InvalidConfigException creating cluster if required attribute (environment) is not on DB`() {
val clusterName = "NameForCluster"
val clusterURL = "UrlForCluster"
val repo = get(Repository::class.java)
every { repo.getCluster(clusterURL) } throws NoResultException()
every { repo.getClusterEnvironment(environmentName) } throws NoResultException()
shouldThrow<InvalidConfigException> {
fetchOrCreateCluster(
clusterName,
environmentName,
clusterURL
)
}
}
}

MockK - cannot mock same function twice

I am trying to test the getTopicNames function (below) in two scenarios: If it succeeds and if it does not succeed.
fun getTopicNames(): Either<Exception, Set<String>> =
try {
adminClient.listTopics()
.names()
.get()
.right()
} catch (exception: ExecutionException) {
exception.left()
}
This is the test class in which I am doing those two scenarios. If I run each test individually, they both suceed. If I run the entire class the second to execute fails because for some reason the previous mock on adminClient.listTopics() is being retained.
These are the versions for everything involved:
kotlin: 1.3.72
koin: 2.1.6
junit: 5.6.1
mockk: 1.10.0
class TopicOperationsTest {
#BeforeEach
fun start() {
val testModule = module(createdAtStart = true) {
single { mockk<AdminClient>() }
}
startKoin { modules(testModule) }
}
#AfterEach
fun stop() {
stopKoin()
}
#Test
fun `getTopicNames() returns a Right with the topics names`() {
val adminClient = get(AdminClient::class.java)
val listOfTopicsToReturn = mockk<ListTopicsResult>()
val expectedTopics = setOf("Topic1", "Topic2", "Topic3")
every { adminClient.listTopics() } returns listOfTopicsToReturn
every { listOfTopicsToReturn.names() } returns KafkaFuture.completedFuture(expectedTopics)
println("listOfTopicsToReturn.names(): " + listOfTopicsToReturn.names())
println("adminClient.listTopics(): " + adminClient.listTopics())
println("getTopicNames(): " + getTopicNames())
assertThat(getTopicNames().getOrElse { emptySet() }, `is`(expectedTopics))
}
#Test
fun `getTopicNames() returns a Left if failing to get topic names`() {
val adminClient = get(AdminClient::class.java)
every { adminClient.listTopics() } throws ExecutionException("Some Failure", Exception())
assertThat(getTopicNames(), IsInstanceOf(Either.Left::class.java))
}
}
This is the error I get, caused by the fact that the test that verifies the failure is the first to run:
java.lang.AssertionError:
Expected: is <[Topic1, Topic2, Topic3]>
but: was <[]>
Expected :is <[Topic1, Topic2, Topic3]>
Actual :<[]>
<Click to see difference>
Already tried clearAllMocks() on the BeforeEach method but it does not solve my problem as I just start getting:
io.mockk.MockKException: no answer found for: AdminClient(#1).listTopics()
I found a solution that makes everything work. It is a combination of:
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
Having the mock as a class object
MockKAnnotations.init(this) in the #BeforeEach method
clearMocks() specifying the actual mock to be cleared (should work for multiple mocks too, just separated by commas.
#TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TopicOperationsTest {
private var adminClientMock = mockk<AdminClient>()
#BeforeEach
fun start() {
MockKAnnotations.init(this)
val testModule = module(createdAtStart = true) {
single { adminClientMock }
}
startKoin { modules(testModule) }
}
#AfterEach
fun stop() {
clearMocks(adminClientMock)
stopKoin()
}
#Test
fun `getTopicNames() returns a Right with the topics names`() {
val adminClient = get(AdminClient::class.java)
val listOfTopicsToReturn = mockk<ListTopicsResult>()
val expectedTopics = setOf("Topic1", "Topic2", "Topic3")
every { adminClient.listTopics() } returns listOfTopicsToReturn
every { listOfTopicsToReturn.names() } returns KafkaFuture.completedFuture(expectedTopics)
assertThat(getTopicNames().getOrElse { emptySet() }, `is`(expectedTopics))
}
#Test
fun `getTopicNames() returns a Left if failing to get topic names`() {
val adminClient = get(AdminClient::class.java)
every { adminClient.listTopics() } throws ExecutionException("Some Failure", Exception())
assertThat(getTopicNames(), IsInstanceOf(Either.Left::class.java))
}
}

startKoin in KoinTest-class throws "A KoinContext is already started"

I'm using "withTestAppliction" in one of my tests to test if the route works. Before all Tests the DB-Table "cats" should have no entries. To get the DAO I need Koin in this Test but if conflicts with "withTestAppliction" where Koin will also be startet and throws A KoinContext is already started
[Update]
I know I could use something like handleRequest(HttpMethod.Delete, "/cats") but I don't want to expose this Rest-Interface. Not even for testing.
#ExperimentalCoroutinesApi
class CatsTest: KoinTest {
companion object {
#BeforeClass
#JvmStatic fun setup() {
// once per run
startKoin {
modules(appModule)
}
}
#AfterClass
#JvmStatic fun teardown() {
// clean up after this class, leave nothing dirty behind
stopKoin()
}
}
#Before
fun setupTest() = runBlockingTest {
val dao = inject<CatDAO>()
dao.value.deleteAll()
}
#After
fun cleanUp() {
}
#Test
fun testCreateCat() {
withTestApplication({ module(testing = true) }) {
val call = createCat(predictName("Pepples"), 22)
call.response.status().`should be`(HttpStatusCode.Created)
}
}
}
fun TestApplicationEngine.createCat(name: String, age: Int): TestApplicationCall {
return handleRequest(HttpMethod.Post, "/cats") {
addHeader(HttpHeaders.ContentType, ContentType.Application.FormUrlEncoded.toString())
setBody(listOf(
"name" to name,
"age" to age.toString()
).formUrlEncode())
}
}
After test (after withTestApplication()) call KoinContextHandler.get().stopKoin().
Example: https://github.com/comm1x/ktor-boot/blob/master/test/common/common.kt
It looks similar to the issue I faced. The problem was that the module() passed under the withTestApplication() was trying to create the Koin object again. I replaced the module() with specific modules that I had to load for the tests except for the Koin.
Refer - test sample and
application sample
Had the same problem executing multiple tests in a class. After removing the init/startKoin (since it's initialized in the Application when you test with the emulator).
I am not really sure if this is the correct approach, but it kind of works for me and my build server.
#ExperimentalCoroutinesApi
#RunWith(AndroidJUnit4ClassRunner::class) // or JUnit4..
class MyTest : KoinTest {
private val mockedAppModule: Module = module(override = true)
factory { myRepo }
}
#Before
fun setup() {
loadKoinModules(mockedAppModule)
}
#After
fun tearDown() {
unloadKoinModules(mockedAppModule)
}
#Test
fun testSubscriberRegistration() = runBlockingTest { // only needed if you are using supend functions
// test impl...
}
}

Mockk: Verify method called within coroutine

I have a simple object which provides a suspend function to simulate a delaying network request and afterwards calls another method from the object.
class CoroutinesObject {
suspend fun doApiCall() {
delay(1000)
println("Hello from API")
val apiResult = "result #1"
callMe(apiResult)
}
fun callMe(result: String) {
println("[${Thread.currentThread().name}] call me with result: $result")
}
}
I would like to write a simple test which should verify that the method callMe has been called.
class CoroutinesTest {
#Test
fun doApiCall_callsCallMe() {
val obj = CoroutinesObject()
runBlocking {
obj.doApiCall()
}
coVerify { obj.callMe("result #1") }
}
}
Unfortunately the test fails with the following exception and I'm not sure why this happens.
io.mockk.MockKException: Missing calls inside verify { ... } block.
Anybody got an idea whats the problem and how to write a test which is able to verify the called method?
Okay, it seems as if a missing mock for my object was the problem. The following test works:
#Test
fun doApiCall_callsCallMe() {
val obj = spyk(CoroutinesObject())
runBlocking {
obj.doApiCall()
}
coVerify { obj.callMe(any()) }
}

Cannot test callbacks with mockk: invoke(any())) was not called

Given
typealias MyCallback = (s: String) -> Unit
object Hello {
fun main() {
blah { print(it) }
}
fun blah(cb: MyCallback) {
cb.invoke("hi")
}
}
Or
interface MyCallback {
fun invoke (s: String) {}
}
object Hello {
fun main() {
blah(object : MyCallback {
override fun invoke(s: String) {
print(s)
}
})
}
fun blah(cb: MyCallback) {
cb.invoke("hi")
}
}
With both I get the above error (Verification failed: call 1 of 1: MyCallback(#2).invoke(any())) was not called) when doing
#Test
fun testInvoke() {
val mock = mockk<Hello>(relaxed = true)
val cb = mockk<MyCallback>()
mock.blah(cb)
verify { cb.invoke(any()) }
}
how to fix it?
This worked for me. The Hello object doesn't need to be mocked since it is the class being tested. By mocking it, the test was only recording invocations of blah() rather than actually executing them.
Using spyk rather than mockk allows the MyCallback type to be constructed allowing the invoke() function to be defined. So maybe that's more of a workaround than an explanation of why mockk doesn't seem to retain that type info.
typealias MyCallback = (s: String) -> Unit
object Hello {
fun main() {
blah { print(it) }
}
fun blah(cb: MyCallback) {
cb.invoke("hi")
}
}
class MockKTest {
#Test
fun testInvoke() {
val mock = spyk<Hello>()
val cb = mockk<MyCallback>(relaxed = true)
mock.blah(cb) // or just do Hello.blah(cb)
verify { cb.invoke(any()) }
}
}