java - 发现@BootstrapWith的多个声明

标签 java spring-boot kotlin

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

@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/

相关文章:

java - 可以在同一应用程序中将 C++ 用作后端,将 Javafx 用作前端吗?

spring-boot - Spring AMQP 多个消费者与更高的预取值

android - 如何在我的 Activity 中存储来自 API 的数据

java - 将自定义 DialogFragment 绑定(bind)到初始化到应用程序范围的服务

kotlin - 克隆 MutableMap 的惯用方法是什么?

java - SAX 解析器不能很好地处理 HTML 片段

java - REST 客户端 InboundJaxrsResponse Status 500 ,当尝试 POST 时

java - 在java中使用非jni native 库

java - 在现有模型对象内传递对象,其中新对象参数未定义并且可能会更改

spring-mvc - 在 Spring Boot 中使用 StandardPasswordEncoder