I'm struggling with providing instances of Beans for SpringBoot tests, can anyone see what I'm doing wrong please:
@SpringBootApplication
class DemoApplication
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
}
@Component
class NormalComponent() {
fun value() = "Hi"
}
Test:
@TestConfiguration
class TestConfig {
@Bean
fun normalComponentMock(): NormalComponent {
val mock = mock(NormalComponent::class.java)
`when`(mock.value()).thenReturn("I AM A MOCK!")
return mock
}
}
@SpringBootTest
@Import(TestConfig::class)
class DemoApplicationTests(
@Autowired val norm: NormalComponent
) {
@Test
fun normInjectedIntoTest() {
assertEquals("I AM A MOCK!", norm.value())
}
}
The error:
org.junit.jupiter.api.extension.ParameterResolutionException: Failed to resolve parameter [com.example.demo.NormalComponent norm] in constructor [public com.example.demo.DemoApplicationTests(com.example.demo.NormalComponent)]: No qualifying bean of type 'com.example.demo.NormalComponent' available: expected single matching bean but found 2: normalComponent,normalComponentMock
I know I could use
@MockBean
- this is just a simple example where the bean I want to inject to the instance.
Bit confused why the mock conflict is happening when I have using the
@TestConfiguration
and the
@Import
annotation - thanks in advance đ