admin管理员组文章数量:1026620
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?
Share Improve this question edited Nov 19, 2024 at 9:02 Anish B. 17.1k4 gold badges28 silver badges50 bronze badges asked Nov 16, 2024 at 17:30 Suraj MenonSuraj Menon 1,6044 gold badges28 silver badges53 bronze badges 1 |1 Answer
Reset to default 1I'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.
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?
Share Improve this question edited Nov 19, 2024 at 9:02 Anish B. 17.1k4 gold badges28 silver badges50 bronze badges asked Nov 16, 2024 at 17:30 Suraj MenonSuraj Menon 1,6044 gold badges28 silver badges53 bronze badges 1-
Can you annotate your field with
@Primary
? – knittl Commented Nov 19, 2024 at 16:17
1 Answer
Reset to default 1I'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.
本文标签: springMockBean with Gradle giving indeterministic class for integration testStack Overflow
版权声明:本文标题:spring - @MockBean with Gradle giving indeterministic class for integration test - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745648718a2161182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
@Primary
? – knittl Commented Nov 19, 2024 at 16:17