Skip to content

Missing documentation on how to migrate from @SpyBean to @MockitoSpyBean with Cucumber #35504

@g4iner

Description

@g4iner

There is a documentation on how to use @MockitoSpyBean, but the problem is how to use it properly with Kotlin. There are only examples with Java.

Before Spring Boot 3.4, I was able to create and use mock with @SpyBean like this:

@Configuration
@SpyBeans(
    SpyBean(SomeProvider::class),
    SpyBean(SecondProvider::class)
)
class TestConfiguration
class TestSteps(
    private val someProvider: SomeProvider,
) {
    @Before
    fun before() {
        doReturn("value").whenever(someProvider).getSomeValue(any())
    }

Now that these annotations are deprecated, I want to migrate them to @MockitoSpyBean.

Should that look like the following?

@Configuration
@MockitoSpyBeans(
   MockitoSpyBean(types = [SomeProvider::class]),
   MockitoSpyBean(types = [SecondProvider::class])
)
class TestConfiguration
class TestSteps(
    private val someProvider: SomeProvider,
) {
    @Before
    fun before() {
        doReturn("value").whenever(someProvider).getSomeValue(any())
    }

With above code the mocks aren't working anymore because of this error:

org.mockito.exceptions.misusing.NotAMockException: 
Argument passed to when() is not a mock!
Example of correct stubbing:
    doThrow(new RuntimeException()).when(mock).someMethod();

Tried this and other solutions like creating mocks in fields inside TestSteps, but nothing worked.

Could you provide documentation on how to migrate the code?

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: invalidAn issue that we don't feel is valid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions