I am stuck on this since two days, while I am trying to implement the Test-resources capability of Micronaut framework.

Here is the content of my application-test.properties :

datasources.default.dialect=MYSQL
datasources.default.driverClassName=com.mysql.cj.jdbc.Driver
datasources.default.schema-generate=CREATE_DROP

test-resources.containers.mysql.image-name=mysql:8.1.0
test-resources.containers.mysql.db-name=testDB
test-resources.containers.mysql.username=dbuser
test-resources.containers.mysql.password="mypassword"
test-resources.containers.mysql.init-script-path="src/test/resources/init.sql"

My pom.xml is declaring the common micronaut dependencies (generated from the Micronaut Launch app).

Anyway, I always have the following error when trying the mvn clean test command.

[test-resources-service] 15:33:37.784 [default-nioEventLoopGroup-1-3] INFO  tc.mysql:8.1.0 - Container is started (JDBC URL: jdbc:mysql://localhost:53931/testDB)
[test-resources-service] 15:33:37.785 [default-nioEventLoopGroup-1-3] WARN  org.testcontainers.ext.ScriptUtils - Could not load classpath init script: "src/test/resources/init.sql"
[test-resources-service] 15:33:37.785 [default-nioEventLoopGroup-1-3] ERROR tc.mysql:8.1.0 - Could not start container
org.testcontainers.ext.ScriptUtils$ScriptLoadException: Could not load classpath init script: "src/test/resources/init.sql". Resource not found.
        at org.testcontainers.ext.ScriptUtils.runInitScript(ScriptUtils.java:347)
        at org.testcontainers.containers.JdbcDatabaseContainer.runInitScriptIfRequired(JdbcDatabaseContainer.java:332)
        at org.testcontainers.containers.JdbcDatabaseContainer.containerIsStarted(JdbcDatabaseContainer.java:187)
        at org.testcontainers.containers.GenericContainer.containerIsStarted(GenericContainer.java:712)
        at org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:532)
        at org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:344)
        at org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:81)
        at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:334)
        at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:322)
        at io.micronaut.testresources.testcontainers.TestContainers.getOrCreate(TestContainers.java:80)
        at io.micronaut.testresources.testcontainers.AbstractTestContainersProvider.resolve(AbstractTestContainersProvider.java:100)
        at io.micronaut.testresources.server.TestResourcesController.resolve(TestResourcesController.java:90)

In the Micronaut documentation it is mentioned that it has to be a file in the classpath. Micronaut Test-Resources doc

My guess here is that the classpth does not use the same root as my java project.

I tried different values for test-resources.containers.mysql.init-script-path.

  • Relative path using the project root as reference
  • Absolute path (not sure it is the best solution, but at least it confirmed that even an absolute path couldn't be resolved)
  • using the classpath: prefix for the relative path of the init script (ex : classpath:src/test/resources/init.sql")
  • using the declaration from the Testcontainers documentation (init script in Classpath & init script from a file

None of them even worked.

Anyway I am sure the other part of the Test-Resources module are working well because when I don't declare an init script, the database is used but none of the expected tables are present.

Am I doing something wrong or missing something in the configuration?

Edit : I am using the standard controller and CrudRepository from Micronaut. I just found appealing to use the Test-Resource for integration purpose.

1

There are 1 best solutions below

0
On

update script path to following it worked for me. after adding script under db folder it works.

src\test\java\resources\db\schema-postgresql.sql

@Container
private static final PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:11.1")
            .withDatabaseName("db").withUsername("test").withPassword("test")
            .withInitScript("db/schema-postgresql.sql");

OR

private static final String initScriptPath="db/schema-postgresql.sql";
ScriptUtils.runInitScript(new JdbcDatabaseDelegate(postgreSQLContainer,""), initScriptPath);

Also make sure that these folder exist under target/test-classes before running Tests. if not than copy manually or setup through maven resource plugin to copy it.