https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

csturtevant

08/24/2020, 8:40 PM
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

okarm

08/24/2020, 8:53 PM
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

csturtevant

08/25/2020, 1:51 AM
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

thanksforallthefish

08/25/2020, 5:50 AM
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

tddmonkey

08/25/2020, 7:05 AM
+1 for what @okarm said. This is a perfect use case for a test double that just returns a canned value
👍 1
c

csturtevant

08/25/2020, 5:14 PM
For right now I am using mockito within my Kotlin test and it runs within ~170ms which is perfect for me. Thanks everyone.