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?

jakstack
  • 2,143
  • 3
  • 20
  • 37
  • 1
    [enter link description here](https://stackoverflow.com/questions/48039644/how-to-make-the-junit-tests-use-the-embedded-mongodb-in-a-springboot-application)Possible duplicate of [this](https://stackoverflow.com/questions/45860899/springboot-how-can-i-perform-an-integration-test-with-real-dependencies) and this – Ankit Sharma Apr 23 '20 at 17:29

1 Answers1

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.

jakstack
  • 2,143
  • 3
  • 20
  • 37