0

I am using testContanier for unit and integration tests.

I need to initialize mongo db with insert document. How can I initialize mongo db before spring boot is runnig ?

@BeforeAll
public static void startAndInitializeContainer(){
 final MongoDBContainer mongoDBContainer = new MongoDBContainer(DockerImageName.parse("mongo:4.0.10"))

mongoDBContainer.start();
}

I have @PostConstruct annotation therefore I need to initialize mongodb under the @BeforeAll annotation.

Any help is appreciated.

GhostDede
  • 438
  • 3
  • 15
  • have you check https://stackoverflow.com/a/73029378/2203890 ? – Eddú Meléndez Dec 14 '22 at 16:51
  • @EddúMeléndez yes ı checked it but, ı can not use additional maven dependency. – GhostDede Dec 15 '22 at 06:37
  • Have you tried using spring test listeners to inseet the docs before the test method runs https://www.baeldung.com/spring-testexecutionlistener – Mark Bramnik Dec 15 '22 at 09:10
  • You can use any Java code you would normally use to add data to MongoDB, there is nothing Testcontainers specific about this (nor should be). – Kevin Wittek Dec 15 '22 at 11:54
  • @KevinWittek I need to insert before spring boot application run. – GhostDede Dec 15 '22 at 13:03
  • I think you need to update your question regarding what do you really need and what have you tried so far. – Eddú Meléndez Dec 15 '22 at 21:16
  • @GhostDede than you have to use a MongoDB client that you construct independent from SpringBoot, e.g., see https://www.mongodb.com/docs/drivers/java/sync/current/usage-examples/insertOne/. However, it is also unclear why you need the data before the SpringBootContext is initialized. – Kevin Wittek Dec 16 '22 at 08:17

0 Answers0