当前位置: 首页 > 知识库问答 >
问题:

找到@BootstrapBy的多个声明

温开畅
2023-03-14

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

@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\u端口,但我还需要来自DataMongoTest的嵌入式mongodb

有什么建议吗?

共有1个答案

单于正业
2023-03-14

我通过这样做来解决它:

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

如果我希望使用mebedded 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()

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

 类似资料:
  • 按照官方文档:http://docs.spring.io/spring-boot/docs/1.4.0.M2/reference/htmlsingle/#Testing 我想这样测试我的一个REST API方法: 如文件所述: 搜索算法从包含测试的包开始,直到找到@SpringBootApplication或@SpringBootConfiguration注释类。只要你以一种合理的方式构造了你的代

  • 我正在处理一个scala项目,这是我的规范: 我知道这里已经有人问过这个问题,但提供的解决办法并不奏效,我很绝望,我试过: 但这并不奏效。

  • 问题内容: 我有弹簧罐,并试图 从此处给出的教程中实现程序。xml配置文件为: 主要: 我不知道这些豆怎么了。到目前为止,关于其他问题的任何建议都无济于事。有什么帮助吗? 问题答案: 假设您使用的是Spring 3.1,请尝试以下操作: 在最后一行用您使用的 主要 Spring版本替换。含义:即使有Spring版本,也没有XSD 。

  • 我正在学习使用rspec在我的木偶模块上进行单元测试,但不知何故,我的简单模块“rspec_test”无法通过测试。该模块只包含一个 init.pp 文件,如下所示: init.pp 以下是执行“$ 佣金规范”的错误消息: 我将测试文件命名为“init_spec.rb ”,内容如下: 和其他文件... 耙 spec_helper.rb 我在模块的根目录中执行了“$ rspec-puppet-ini

  • 我有这个问题 XML格式的文档 我的XSD 我有个神经衰弱的问题。有什么想法吗? 问候

  • 在spring中,每当我在dispatcher-servlet.xml中编写时,我都会得到这样的错误:-