flapdoodle-oss / de.flapdoodle.embed.mongo.spring

embedded mongo spring integration
Apache License 2.0
30 stars 7 forks source link

Can i use embed mongo and spring autoconfigured mongo both? #25

Closed plzprayme closed 8 months ago

plzprayme commented 1 year ago

Hello, michael! Thank you for share this awesome project to the world.

I just want a use embed mongo with spring autoconfigured mongo. can you give me a hint?

my hope example:

@SpringBootTest(
    properties = [
        "de.flapdoodle.mongodb.embedded.version=4.4.22",
    ]
)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
class EmbeddedMongoTestContext {
  @Test
  fun testSomething() { /** TODO **/ }
}

class EmbeddedMongoTest(
    private val simpleRepository: SimpleRepository
) : EmbeddedMongoTestContext() {
    @Test
    fun example() {
        // get data from embed mongo db
        println(simpleRepository.findAll())
    }
}

@SpringBootTest(
    properties = [
        "spring.data.mongodb.uri=mongodb://....",
    ]
)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
class RealMongoTestContext {
  @Test
  fun testSomething() { /** TODO **/ }
}

class RealMongoTest(
    private val simpleRepository: SimpleRepository
) : RealMongoTestContext() {
    @Test
    fun example() {
        // get data from real mongo db
        println(simpleRepository.findAll())
    }
}

There is no way to disable autoconfiguration options? I just want a create embed mongo, spring autoconfigured mongo by myself.

Thank you.

michaelmosmann commented 1 year ago

@plzprayme .. hmm.. i guess you have to exclude the configuration class as i had to do with spring 2.7.x .. where this annotation is on my autoconfig class:

@AutoConfigureBefore({ MongoAutoConfiguration.class, org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration.class })

... i have to think about this..

plzprayme commented 1 year ago

@michaelmosmann Thank you for your hint!

I reoslve it like this.

@SpringBootTest(
    properties = [
        "de.flapdoodle.mongodb.embedded.version=4.4.22",
    ]
)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
class EmbeddedMongoTestContext {
  @Test
  fun testSomething() { /** TODO **/ }
}

class EmbeddedMongoTest(
    private val simpleRepository: SimpleRepository
) : EmbeddedMongoTestContext() {
    @Test
    fun example() {
        // get data from embed mongo db
        println(simpleRepository.findAll())
    }
}

@EnableAutoConfiguration( exclude = [EmbeddedMongoAutoConfiguration::class] ) << just add this line
@SpringBootTest(
    properties = [
        "spring.data.mongodb.uri=mongodb://....",
    ]
)
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
class RealMongoTestContext {
  @Test
  fun testSomething() { /** TODO **/ }
}

class RealMongoTest(
    private val simpleRepository: SimpleRepository
) : RealMongoTestContext() {
    @Test
    fun example() {
        // get data from real mongo db
        println(simpleRepository.findAll())
    }
}

How about add this use case on spring-3.0.x ?

michaelmosmann commented 1 year ago

@plzprayme thanks.. i will see if i can add this to the documentation .. :)

michaelmosmann commented 8 months ago

@plzprayme Done:)