Annotation @RunWith(MockitoJUnitRunner::class) is not initializing mocks in Kotlin

1.4k Views Asked by At

I am trying to write a test class in Kotlin with org.jetbrains.kotlin:kotlin-test-junit:1.4.31 in my build.gradle.

In my test class, mocks are not getting initialized via the '@RunWith' annotation i.e. :

@RunWith(MockitoJUnitRunner::class)

e.g. I am using

@InjectMocks lateinit var bookService: BookServiceImpl

For above mocks to get initialized I have to use 'MockitoAnnotations.initMocks(this)' (otherwise I get exception) e.g.

@InjectMocks lateinit var bookService: BookServiceImpl

@Test
fun testStudentGenerationEvent(){
    MockitoAnnotations.initMocks(this)
    val student = MyTestUtil.getStudentDTO()
    bookService.prepareStudentGenerationEvent(customerData, "test-student-topic")
}

can anyone suggest why above behavior is happening? Why @RunWith(MockitoJUnitRunner::class) is not initializing the mock ( I get exception

    lateinit property bookService has not been initialized kotlin.UninitializedPropertyAccessException: lateinit property bookService has not been initialized

) and only MockitoAnnotations.initMocks(this) is able to initialize.

0

There are 0 best solutions below