Ninja-Squad/springmockk

@MockkBean should work in the constructor

mikrethor opened this issue · 2 comments

To avoid the use of lateinit var I have taken the habit of declaring my beans in the constructor. Like the following :

`import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(MockKExtension::class)
internal class ABeanUsingMyBean5Test(@mockk val myBean: MyBean) {

lateinit var aBeanUsingMyBean: ABeanUsingMyBean

@BeforeEach
fun beforeEach() {
    aBeanUsingMyBean = ABeanUsingMyBean(myBean)
}

@Test
fun description() {
    val description = "a mockked description"
    every { myBean.description } returns description
    Assertions.assertThat(aBeanUsingMyBean.myDescription()).isEqualTo(description)
}

}`

So for the following code, it would work :

`import com.ninjasquad.springmockk.MockkBean
import io.mockk.every
import io.mockk.junit5.MockKExtension
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.test.context.junit.jupiter.SpringExtension

@ExtendWith(MockKExtension::class)
@ExtendWith(SpringExtension::class)
internal class ABeanUsingMyBean6Test(@MockkBean val myBean: MyBean) {

lateinit var aBeanUsingMyBean: ABeanUsingMyBean

@BeforeEach
fun beforeEach() {
    aBeanUsingMyBean = ABeanUsingMyBean(myBean)
}

@Test
fun description() {
    val description = "a mockked description"
    every { myBean.description } returns description
    Assertions.assertThat(aBeanUsingMyBean.myDescription()).isEqualTo(description)
}

}`

Don't know if it's a bug or a feature request.

Hi @mikrethor.

Thanks for your feature request, but I don't plan to have features that the standard support for Mockito provided by Spring Boot doesn't also have.

If Spring Boot decides to add this feature to its MockBean annotation handling, then I'll port the feature here. But not before.

Hello @jnizet I understand. I am gonna add a feature request to the Spring Boot project and if they do I will comment to repoen this issue.