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
openkeyword 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 :) )