1

I'm using a base integration test with these annotations:

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

When I run the test i get this error:

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)] 

The error is caused by both annotations @SpringBootTest and @DataMongoTest including @BootstrapWith like this:

@SpringBootTest has @BootstrapWith(SpringBootTestContextBootstrapper.class) @DataMongoTest has @BootstrapWith(DataMongoTestContextBootstrapper.class) 

I need to keep using @SpringBootTest for SpringBootTest.WebEnvironment.RANDOM_PORT but i also want the embedded mongodb from @DataMongoTest

Any suggestions?

1

1 Answer 1

1

I solved it by doing this:

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

In cases where I want the mebedded mongodb to be used:

@EmbeddedKafka( partitions = 3, controlledShutdown = false, brokerProperties = ["listeners=PLAINTEXT://127.0.0.1:9092", "port=9092"]) abstract class IntegrationTestBase: SomeAppSpringBootTest() { 

In the above you get the embedded mongodb by just including:

testCompile("de.flapdoodle.embed:de.flapdoodle.embed.mongo:2.2.0") 

In cases where I don't wan't the embedded behaviour i use:

@TestPropertySource(properties = ["spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration"]) abstract class EndToEndTestBase: SomeAppSpringBootTest() 

So for each type of test i use the correct abstract base test class.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.