Has anyone else here been running into long start ...
# getting-started
c
Has anyone else here been running into long start up times for unit tests that use mockks? It takes 2-3 seconds compared to a unit test without mocks running in ~250ms. I’m on Kotlin 1.4.0 and Intellij 2020.2 with a JUnit 5 unit test. The goal is to have all unit tests running around ~300ms if possible. The mockk unit test is below:
Copy code
import io.mockk.every
import io.mockk.mockk


import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class ViewControllerTest {

    private val repository: TaskRepository = mockk()
    private lateinit var viewController : ViewController

    @BeforeEach
    fun setUp() {
        viewController = ViewController(repository)
        every { repository.findTasksFromView(any()) } returns emptyList()
    }

    @Test
    fun getView() {
        val list = viewController.getView(1)
        println(list)
    }
}
The regular unit test is here:
Copy code
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertAll
import kotlin.test.assertEquals

internal class TaskTest {

    private val project: Project = Project(1, "Inbox","The default project", emptyList())

    @BeforeEach
    fun setUp() {
    }

    @Test
    fun `maps Task to Viewable using extension function`() {
        val task : Task = buildTask()
        val viewable : Viewable = task.toViewable()
        assertViewable(viewable)
    }

    private fun buildTask() : Task {
        return Task(
                1,
                "Get focused",
                "TODO",
                project
        )
    }

    private fun assertViewable(viewable: Viewable) {
        assertAll(
                {assertEquals(1, viewable.id)},
                {assertEquals("Get focused", viewable.name)},
                {assertEquals("TODO", viewable.status)},
                {assertEquals(project, viewable.project)},
        )
    }
}
Someone else is having the same issue on StackOverflow: https://stackoverflow.com/questions/62208145/why-is-mocking-so-slow-to-start-in-kotlin
o
Hmm, I don't know about slow `Mock`s, but have you considered solving the problem by using a different type of test double, such as a Fake instead? In my experience a fake repository interface implementation backed by an array or a list instead of database is very much superior to a repository mock.
👍 1
c
I’m sure a fake would work just as well, I just felt that the mock should work as expected and quickly, but I’ll look into other test runners like Kotest and if that doesn’t work I might just right the unit tests in java instead when using Mocks, because that’s just too slow. Thanks for taking a look.
t
kotest would not help much, as kotest does not deal in mocks afaik
👍 1
I had the same, somehow did not investigate further: https://kotlinlang.slack.com/archives/C9EJFT6DB/p1592901300100200 mocking is expensive, as it relies (probably) on proxies which are instantiated by reflectively scanning a class. but to me it looked too slow.
👍 1
t
+1 for what @okarm said. This is a perfect use case for a test double that just returns a canned value
👍 1
c
For right now I am using mockito within my Kotlin test and it runs within ~170ms which is perfect for me. Thanks everyone.