Reputation: 45
I have a Spring Boot test to check if the Gradle embedded Postgres database has been successfully created. I'm using TestContainers with the JDBC URL and an init function for liquibase.
public class TestcontainersPgIT {
public static final String EMBEDDED_JDBC_URL = "jdbc:tc:postgresql:12.5://localhost:5432/postgres?TC_INITFUNCTION=<package names>.db.LiquibaseRunner::runUpdate";
@Test
public void testEmbeddedPg() throws Exception {
Connection conn = DriverManager.getConnection(EMBEDDED_JDBC_URL);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select 1");
assertTrue(rs.next());
String result = rs.getString(1);
assertEquals("1", result);
}
What happens above is that the line Connection conn =...
will end up calling my liquibase runner function below.
public final class LiquibaseRunner {
private LiquibaseRunner() {
}
public static void runUpdate(Connection connection) throws LiquibaseException {
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new liquibase.Liquibase("src/test/resources/db/changelog/db.changelog-test.xml", new FileSystemResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());
The line that creates the new liquibase instance and passes in a path ( Liquibase liquibase = new...
) to the changelog file is the source off these errors in any Liquibase version >= 4.0. It seems unable to find the changelog file inside my tests/resources/db/changelog
directory.I know this directory is copied over into the JAR/Build when it is compiled so I know the files are there. The error it throws is shown below.
Specifying files by absolute path was removed in Liquibase 4.0. Please use a relative path or add '/' to the classpath parameter.
at liquibase.parser.core.xml.XMLChangeLogSAXParser.parseToNode(XMLChangeLogSAXParser.java:82)
at liquibase.parser.core.xml.AbstractChangeLogParser.parse(AbstractChangeLogParser.java:15)
at liquibase.Liquibase.getDatabaseChangeLog(Liquibase.java:377)
at liquibase.Liquibase.lambda$update$1(Liquibase.java:230)
at liquibase.Scope.lambda$child$0(Scope.java:160)
at liquibase.Scope.child(Scope.java:169)
at liquibase.Scope.child(Scope.java:159)
at liquibase.Scope.child(Scope.java:138)
at liquibase.Liquibase.runInScope(Liquibase.java:2369)
at liquibase.Liquibase.update(Liquibase.java:217)
at liquibase.Liquibase.update(Liquibase.java:203)
I've tried many different file paths to be passed in, including using class path
and relative file paths and it's still unable to find the changelog file for integration tests.
Filepaths I've tried
classpath:db/changelog/db.changelog-test.xml
classpath:/db/changelog/db.changelog-test.xml
Upvotes: 0
Views: 2500
Reputation: 1
Use a ClassLoaderResourceAccessor Instead of the FileSystemResourceAccessor:
ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor();
Then, just access your changelog relatively to your resource folder:
new Liquibase("db/changelog/db.changelog-test.xml")
Upvotes: 0
Reputation: 21
You may just need this path resources/db/changelog/db.changelog-test.xml
or this path: ../test/resources/db/changelog/db.changelog-test.xml
.
Upvotes: 0