0

I am trying to test class B's otherWork function but beacuse of its parent class' property I am getting lateinit property p has not been initialized error

class A{
    @Autowired
    private lateinit var p:Propery
    
    fun work(){
        p.doSomething()
    }
}

class B:A{
    fun otherWork(){
        work()
    }
}

@ExtendWith(MockitoExtension::class)
class Test{
    @MockkBean
    lateinit var p: Property

    @BeforeEach
    fun setup(){
        every { p.doSomething(any()) } returns Unit
    }
    
    @Test
    fun t(){
        /// tests
    }
}

The above approach didn't fix the issue. For my test A's work function is unimportant. How can I omit for the test?

1 Answers1

0

The behavior you're seeing is best explained by this answer. In a nutshell, @MockBean and @Autowired are not processed by MockitoExtension. You need to use both MockitoExtension and SpringExtension:

@ExtendWith(MockitoExtension::class)
@ExtendWith(SpringExtension::class)
class Test { ... }

Alternatively, you can explicitly define the repeatable annotation @Extensions. From what I remember, earlier versions of Kotlin had some interoperability problems with Java's repeatable annotations, so this may be necessary:

@Extensions(
    ExtendWith(MockitoExtension::class),
    ExtendWith(SpringExtension::class)
)
class Test { ... }
Brian
  • 17,079
  • 6
  • 43
  • 66