Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject a list of implementations in a @InjectMockKs test instance?

Spring Boot allows to inject a list of all implementations of an interface (SomeComponent) as List into another component (SomeOtherComponent), e.g.

@Component
interface SomeComponent
@Component
class SomeComponentImpl0 : SomeComponent
@Component
class SomeComponentImpl1 : SomeComponent
class SomeOtherComponent {
    @Autowired
    lateinit var impls: List<SomeComponent>
}

How can I inject mocks for the implementations using MockK annotations? In

import io.mockk.MockKAnnotations
import io.mockk.impl.annotations.InjectMockKs
import io.mockk.impl.annotations.MockK
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test

class SomeOtherComponentTest {
    @MockK
    lateinit var someComponentImpl0: SomeComponentImpl0

    @MockK
    lateinit var someComponentImpl1: SomeComponentImpl1

    @InjectMockKs
    lateinit var instance: SomeOtherComponent

    @BeforeEach
    fun setup() {
        MockKAnnotations.init(this)
    }

    @Test
    fun testSomething() {
        println(instance.impls.toString())
    }
}

I'm getting either

io.mockk.MockKException: 
No matching constructors found:
constructor(impls : kotlin.collections.List<de.richtercloud.inject.mocks.foor.list.of.impl.SomeComponent> = <not able to lookup>)
        at de.richtercloud.inject.mocks.foor.list.of.impl.SomeOtherComponentTest.setup(SomeOtherComponentTest.kt:40)

if I'm using constructor injecction and

kotlin.UninitializedPropertyAccessException: lateinit property impls has not been initialized
        at de.richtercloud.inject.mocks.foor.list.of.impl.SomeOtherComponentTest.testSomething(SomeOtherComponentTest.kt:26)

if I'm using an @Autowired var property in the class.

I'm using 1.3.50 through Maven 3.6 and MockK 1.9.3.

like image 869
Kalle Richter Avatar asked Oct 15 '22 11:10

Kalle Richter


1 Answers

Add @ExtendWith(MockKExtension::class) above your Test class to make @InjectMokKs work.

like image 111
Fabi D Avatar answered Oct 20 '22 17:10

Fabi D