Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mockito in Micronaut and kotin not working

Somehow I'm not able to mock via mockito a method and getting null pointer. Not sure why its failing!

import io.github.xxxx.repository.ProductRepository
import io.micronaut.test.annotation.MockBean
import io.micronaut.test.extensions.junit5.annotation.MicronautTest
import org.assertj.core.api.Assertions.assertThat
import org.bson.Document
import org.junit.jupiter.api.Test
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import javax.inject.Inject

@MicronautTest
open class OrderServiceTest {

    @field:Inject
    lateinit var productRepository: ProductRepository

    @Test
    fun name() {
        assertThat(2+3).isEqualTo(5)
        // 👇 here I'm getting the error, The method is of type T? so returning null is ok
        `when`(productRepository.find("abc")).thenReturn(null);
         assertThat(productRepository.find("abc")).isNull()
    }

    @MockBean(ProductRepository::class)
    open fun contextService(): ProductRepository {
        return mock(ProductRepository::class.java)
    }
}

The exception that I'm getting:

java.lang.NullPointerException
    at io.github.imalik8088.repository.ProductRepository.getCollection(ProductRepository.kt:65)
    at io.github.imalik8088.repository.ProductRepository.find(ProductRepository.kt:40)
    at io.github.imalik8088.service.OrderServiceTest.name(OrderServiceTest.kt:22)

Addition/Solution/Findings:

  • I found that Kotest the way to go with Micronaut framework and kotlin AFAIU Not if testcontainers with the junit extentions that I'm using is working with that
  • mockk for mocking because native and idiomatic. Mockito has issues with final classes AFAIK e.g. Spring-Boot solves it with a lib that internally added open keyword to Kotlin classes
  • Found also somehow the docs incomplete or not very precise there imports or methods used from kotest that are deprecated and in the source code it mentioned to use kotest..
  • For the side that I'm doing it is not very important busy for a production critical it would helpful - my 2cents ... could be also lack of knowledge in the Framework and language (noob status for both :) )
like image 294
imalik8088 Avatar asked Dec 21 '25 08:12

imalik8088


1 Answers

if you working with kotlin i recommend you to use mockk (https://mockk.io/) quite similar to mockito but using the full featureset of kotlins dsl. Regarding your problem, from the stacktrace it seems to me, that the mock does not work. Workin with repos i always recommend to use testcontainer and not mock the repos, cause in reality it could cause a lot of trouble, when the generated sql not works as expected.

But if you want to mock your repo and run some tests on that. Forget about @MicronautTest and just mock the interface like that:

internal class OrderServiceTest {

private val  productRepository: ProductRepository = mock(ProductRepository::class.java)

@Test
fun name() {
    assertThat(2+3).isEqualTo(5)
    `when`(productRepository.find("abc")).thenReturn(null);
     assertThat(productRepository.find("abc")).isNull()
}

}

like image 195
IEE1394 Avatar answered Dec 22 '25 21:12

IEE1394



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!