Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poor performance with lateinit mockks #1231

Open
ak131554 opened this issue Mar 6, 2024 · 0 comments
Open

Poor performance with lateinit mockks #1231

ak131554 opened this issue Mar 6, 2024 · 0 comments

Comments

@ak131554
Copy link

ak131554 commented Mar 6, 2024

I've recently discovered that our tests are needlessly slow. By investigation I found out that the following pattern is slow.

class TestWithLateinitMockk
{
  private lateinit var one: AtomicInteger

  @BeforeEach
  fun setup()
  {
    one = mockk()
    every { one.get() } returns 1
  }

  @Test
  fun testIt()
  {
    Assertions.assertEquals(3, 2 + one.get())
  }
}

In contrary, I have three different Tests:

class BasicTest
{
  private var one: AtomicInteger = AtomicInteger(1)

  @Test
  fun testIt()
  {
    Assertions.assertEquals(3, 2 + one.get())
  }
}

class TestWithLateinit
{
  private lateinit var one: AtomicInteger

  @BeforeEach
  fun setup()
  {
    one = AtomicInteger(1)
  }

  @Test
  fun testIt()
  {
    Assertions.assertEquals(3, 2 + one.get())
  }
}

class TestWithMockk
{
  private var one: AtomicInteger = mockk()

  @BeforeEach
  fun setup()
  {
    every { one.get() } returns 1
  }

  @Test
  fun testIt()
  {
    Assertions.assertEquals(3, 2 + one.get())
  }
}

Each of those three tests run on my machine in low (one or two digits) milliseconds. The lateinit mockk tests needs more than one second for the same Assertion. So it's several orders of magnitude slower. (Please see the working code in my Gitlab Repository, so that you can try it yourself.)

I don't know if this is a bug. If not than maybe someone can explain to me, why lateinit mockks have such an impact on performance. I know that mock(k)ing is a bit slower due to reflection. But I don't understand the difference between initialize on declaration and late initialized mockks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant