testcontainerstestcontainers-junit5

How do we skip executing init script on reusable container?


When using reusable container with init script, it runs the script every time the test start even though same DB is being reused. In init script I have create and insert statements so every time I run the test, it tries to execute the sql statements and fails with table already exists.


Solution

  • You can execute init scripts manually by using testcontainers class JdbcDatabaseDelegate instead of calling for postgresContainer.withInitScripts() before the startup.

    For example:

    JdbcDatabaseDelegate delegate = new JdbcDatabaseDelegate(postgresContainer, "");
    ScriptUtils.runInitScript(delegate, sqlScriptFilePath));