gpt4 book ai didi

java - 发现@BootstrapWith的多个声明

转载 作者:行者123 更新时间:2023-12-02 13:30:52 29 4
gpt4 key购买 nike

我正在使用带有这些注释的基本集成测试:

@ExtendWith(SpringExtension::class)
@SpringBootTest(classes = [SomeApplication::class], webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DataMongoTest
@EmbeddedKafka(
partitions = 3,
controlledShutdown = false,
brokerProperties = ["listeners=PLAINTEXT://127.0.0.1:9092", "port=9092"])
abstract class IntegrationTestBase {

当我运行测试时,出现此错误:
java.lang.IllegalStateException: Configuration error: found multiple declarations of @BootstrapWith for test class [com.some.app.application.restapi.DatabaseSetupApiEte]: [@org.springframework.test.context.BootstrapWith(value=org.springframework.boot.test.context.SpringBootTestContextBootstrapper), @org.springframework.test.context.BootstrapWith(value=org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTestContextBootstrapper)]

该错误是由包括 @SpringBootTest的批注 @DataMongoTest@BootstrapWith引起的
像这样:
@SpringBootTest has @BootstrapWith(SpringBootTestContextBootstrapper.class)

@DataMongoTest has @BootstrapWith(DataMongoTestContextBootstrapper.class)

我需要继续使用 @SpringBootTest作为 SpringBootTest.WebEnvironment.RANDOM_PORT,但我也想从 @DataMongoTest中嵌入嵌入式mongodb

有什么建议么?

最佳答案

我这样做解决了:

@ExtendWith(SpringExtension::class)
@SpringBootTest(classes = [SomeAppApplication::class], webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
abstract class SomeAppSpringBootTest {

在我要使用嵌入式mongodb的情况下:
@EmbeddedKafka(
partitions = 3,
controlledShutdown = false,
brokerProperties = ["listeners=PLAINTEXT://127.0.0.1:9092", "port=9092"])
abstract class IntegrationTestBase: SomeAppSpringBootTest() {

在上面的代码中,您只需包含以下内容即可获得嵌入式mongodb:
testCompile("de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.2.0") 

如果我不需要嵌入式行为,则可以使用:
@TestPropertySource(properties = ["spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration"])
abstract class EndToEndTestBase: SomeAppSpringBootTest()

因此,对于每种类型的测试,我都使用正确的抽象基础测试类。

关于java - 发现@BootstrapWith的多个声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61387890/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com