File tree Expand file tree Collapse file tree 4 files changed +34
-46
lines changed
main/java/org/testcontainers
test/java/org/testcontainers/mongodb Expand file tree Collapse file tree 4 files changed +34
-46
lines changed Original file line number Diff line number Diff line change @@ -223,23 +223,4 @@ void withSharding() {
223223 setEntrypoint ("sh" );
224224 }
225225 }
226-
227- /**
228- * Executes a MongoDB initialization script from the classpath during startup.
229- * <p>
230- * The script will be copied to {@code /docker-entrypoint-initdb.d/init.js}.
231- * This method also adjusts the {@link org.testcontainers.containers.wait.strategy.WaitStrategy}
232- * to expect the "waiting for connections" log message twice, as the execution of an init script
233- * causes MongoDB to restart.
234- *
235- * @param scriptPath the path to the init script file on the classpath
236- * @return this container instance
237- */
238- public MongoDBContainer withInitScript (String scriptPath ) {
239- withCopyFileToContainer (MountableFile .forClasspathResource (scriptPath ), "/docker-entrypoint-initdb.d/init.js" );
240-
241- this .waitStrategy = Wait .forLogMessage ("(?i).*waiting for connections.*" , 2 );
242-
243- return this ;
244- }
245226}
Original file line number Diff line number Diff line change @@ -204,4 +204,23 @@ public String getReplicaSetUrl(String databaseName) {
204204 }
205205 return getConnectionString () + "/" + databaseName ;
206206 }
207+
208+ /**
209+ * Executes a MongoDB initialization script from the classpath during startup.
210+ * <p>
211+ * The script will be copied to {@code /docker-entrypoint-initdb.d/init.js}.
212+ * This method also adjusts the {@link org.testcontainers.containers.wait.strategy.WaitStrategy}
213+ * to expect the "waiting for connections" log message twice, as the execution of an init script
214+ * causes MongoDB to restart.
215+ *
216+ * @param scriptPath the path to the init script file on the classpath
217+ * @return this container instance
218+ */
219+ public MongoDBContainer withInitScript (String scriptPath ) {
220+ withCopyFileToContainer (MountableFile .forClasspathResource (scriptPath ), "/docker-entrypoint-initdb.d/init.js" );
221+
222+ this .waitStrategy = Wait .forLogMessage ("(?i).*waiting for connections.*" , 2 );
223+
224+ return this ;
225+ }
207226}
Original file line number Diff line number Diff line change 22
33import org .junit .jupiter .api .Test ;
44
5+ import java .time .Duration ;
6+
57import static org .assertj .core .api .Assertions .assertThat ;
68
79class MongoDBContainerTest extends AbstractMongo {
@@ -38,4 +40,17 @@ void shouldTestDatabaseName() {
3840 assertThat (mongoDBContainer .getReplicaSetUrl (databaseName )).endsWith (databaseName );
3941 }
4042 }
43+
44+ @ Test
45+ void testWithInitScript () {
46+ try (
47+ MongoDBContainer mongoDB = new MongoDBContainer ("mongo:4.0.10" )
48+ .withInitScript ("init.js" )
49+ .withStartupTimeout (Duration .ofSeconds (30 ))
50+ ) {
51+ mongoDB .start ();
52+
53+ assertThat (mongoDB .isRunning ()).isTrue ();
54+ }
55+ }
4156}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments