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

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

import a json file containing mongo documents into Embedded DB in Spring #29

Closed rajakumare1 closed 11 months ago

rajakumare1 commented 1 year ago

I have 100's of documents stored inside sample.json and I want to load it in spring project Junit while starting the embeded mongodb. using the below reference , how should I do it. Please advise?

Note: I referred the import section in https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/embed-mongo-4.x/Howto.md. But i didnt understand how I can integrate it with Spring support.

I have multiple collections with multiple records. Tats why I am looking for this support that will help rather than inserting each record for every Junit. Thanks!!!

`@ExtendWith(SpringExtension.class) @activeprofiles("test") @SpringBootTest( properties = "de.flapdoodle.mongodb.embedded.version=5.0.12" ) @Import({TestMongoConfiguration.class}) @AutoConfigureDataMongo() @testinstance(TestInstance.Lifecycle.PER_CLASS) @DirtiesContext class ServiceIntegrationTest {

@Autowired private TestCollectionRepository testCollectionRepository;

@BeforeEach void setup() throws IOException { TestCollection testcollection = populateMockTestCollection(); testCollectionRepository.insert(testcollection); }

......

} @TestConfiguration

public class TestMongoConfiguration {

@Bean public MongoDatabaseFactory mongoDbFactory() throws Exception { return new SimpleMongoClientDatabaseFactory("mongodb://localhost:27017/db-tests"); }

@Bean MongoTemplate mongoTemplate(MongoDatabaseFactory factory) { return new MongoTemplate(factory); } } `

michaelmosmann commented 1 year ago

@rajakumare1 i will improve the documentation on that .. because it is not that easy ..

you need to customize your mongodb-starter: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/blob/main/HowTo.md#customize-mongod where you trigger only an import: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/embed-mongo-4.x/Howto.md#import-json-file-with-mongoimport-command ...

but you should wait for a working sample for that.. (maybe today)

rajakumare1 commented 1 year ago

@michaelmosmann Any luck with the sample. Thanks!!

michaelmosmann commented 1 year ago

@rajakumare1 not finished yet.. was in a hurry.. stay tuned.. sorry