我正在使用带有这些注释的基本集成测试:
@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/