I am using the following for my integration test
@ExtendWith(SpringExtension::class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = [TestSecurityConfig::class],
properties = ["spring.main.allow-bean-definition-overriding=true"],
)
@AutoConfigureMockMvc
class ImportControllerIT {
@MockBean
private lateinit var restClientImpl: RestClientImpl
@MockBean
private lateinit var fileManagerImpl: FileManagerImpl
@Autowired
private lateinit var mockMvc: MockMvc
....
@BeforeAll
fun setup() {
whenever(restClientImpl.getFileContent(uuid, null)).thenReturn(getFileContent("employee.csv"))
RestAssured.port = port
}
Wherever I run the code using IntellJ, I see the Mock Bean being used, but when I test the behaviour using gradle command like bash ./gradlew -i integrationTest
I see the mock bean and the actual bean being picked randomly.
What am I doing wrong?
I am using the following for my integration test
@ExtendWith(SpringExtension::class)
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
classes = [TestSecurityConfig::class],
properties = ["spring.main.allow-bean-definition-overriding=true"],
)
@AutoConfigureMockMvc
class ImportControllerIT {
@MockBean
private lateinit var restClientImpl: RestClientImpl
@MockBean
private lateinit var fileManagerImpl: FileManagerImpl
@Autowired
private lateinit var mockMvc: MockMvc
....
@BeforeAll
fun setup() {
whenever(restClientImpl.getFileContent(uuid, null)).thenReturn(getFileContent("employee.csv"))
RestAssured.port = port
}
Wherever I run the code using IntellJ, I see the Mock Bean being used, but when I test the behaviour using gradle command like bash ./gradlew -i integrationTest
I see the mock bean and the actual bean being picked randomly.
What am I doing wrong?
I'm not 100% sure but it looks like spring.main.allow-bean-definition-overriding=true
is causing the randomness of picking the mock bean and the actual bean.
Recommendation: Avoid doing the override of bean definition especially in tests.
Solution: Fix your implementation so that you don't need to override any kind of bean definitions in tests.
@Primary
? – knittl Commented Nov 19, 2024 at 16:17