micronauttestcontainersmicronaut-datatestcontainers-junit5micronaut-test

Bean definition [javax.sql.DataSource] could not be loaded: Unsupported Database: PostgreSQL 17.2 + Test Resources plugin


Using Test resources in Micronaut application with below configuration

plugins {
    id("com.github.johnrengelman.shadow") version "8.1.1"
    id("io.micronaut.application") version "4.4.4"
    id("io.micronaut.test-resources") version "4.4.4" <-- Added here
    id("io.micronaut.aot") version "4.4.4"
    id("org.flywaydb.flyway") version "11.0.1"
}

Dependencies

dependencies {
    annotationProcessor("org.projectlombok:lombok")
    annotationProcessor("io.micronaut:micronaut-http-validation")
    annotationProcessor("io.micronaut.data:micronaut-data-processor")

    implementation("io.micronaut:micronaut-jackson-databind")
    implementation("school.staff:common:0.0.1")
    implementation("io.micronaut.data:micronaut-data-jdbc")
    implementation("io.micronaut:micronaut-management")
    implementation("org.flywaydb:flyway-core:11.1.0")
    implementation("io.micronaut.flyway:micronaut-flyway")

    compileOnly("org.projectlombok:lombok")
    compileOnly("io.micronaut:micronaut-http-client")
    compileOnly("jakarta.persistence:jakarta.persistence-api")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("org.postgresql:postgresql")
    runtimeOnly("io.micronaut.sql:micronaut-jdbc-hikari")

    testImplementation("io.micronaut:micronaut-http-client")
    testImplementation("net.datafaker:datafaker:2.4.2")
}

Configuration

public abstract class TestContainerConfig implements TestPropertyProvider {
    @Override
    public @NonNull Map<String, String> getProperties() {
        return Map.of(
                "datasources.default.dialect", "POSTGRES",
                "datasources.default.db-type", "postgres",
                "datasources.default.driver-class-name", "org.postgresql.Driver"
        );
    }
}

BaseFixture extends TestContainerConfig

public class BaseFixture extends TestContainerConfig{
    protected final TestDataGenerator testDataGenerator = new TestDataGenerator();
}

Each test case is extending baseFixture

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@MicronautTest
public final class TagServiceTest extends BaseFixture {
    @Inject
    private IServiceAction<TagRequest, TagResponse> tagService;


    @Test
    @DisplayName("Should create a tag with valid name")
    void shouldCreateATagWithValidName() {
        // Give
        TagRequest requestModel = testDataGenerator.generateRecord(TagRequest.class);
        // When
        var result =  this.tagService.post(requestModel);

        //Then
        assertNotNull(result);
    }

application-test.properties

# Run migrations manually
flyway.datasources.default.enabled=true

When running the test case facing the below issue

19:16:41.161 [Test worker] INFO  i.m.c.DefaultApplicationContext$RuntimeConfiguredEnvironment - Established active environments: [test]
19:16:42.457 [Test worker] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
19:16:42.560 [Test worker] INFO  com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@77b919a3
19:16:42.561 [Test worker] INFO  com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
19:16:42.608 [Test worker] INFO  i.m.flyway.AbstractFlywayMigration - Running migrations for database with qualifier [default]

Bean definition [javax.sql.DataSource] could not be loaded: Unsupported Database: PostgreSQL 17.2
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [javax.sql.DataSource] could not be loaded: Unsupported Database: PostgreSQL 17.2
    at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:2000)
    at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:314)
    at io.micronaut.context.DefaultBeanContext.configureAndStartContext(DefaultBeanContext.java:3318)
    at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:345)
    at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:216)
    at io.micronaut.test.extensions.AbstractMicronautExtension.startApplicationContext(AbstractMicronautExtension.java:507)
    at io.micronaut.test.extensions.AbstractMicronautExtension.beforeClass(AbstractMicronautExtension.java:346)
    at io.micronaut.test.extensions.junit5.MicronautJunit5Extension.beforeAll(MicronautJunit5Extension.java:84)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: org.flywaydb.core.api.FlywayException: Unsupported Database: PostgreSQL 17.2
    at org.flywaydb.core.internal.database.DatabaseTypeRegister.lambda$getDatabaseTypeForConnection$7(DatabaseTypeRegister.java:131)
    at java.base/java.util.Optional.orElseThrow(Optional.java:403)
    at org.flywaydb.core.internal.database.DatabaseTypeRegister.getDatabaseTypeForConnection(DatabaseTypeRegister.java:131)
    at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:77)
    at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:137)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:178)
    at io.micronaut.flyway.AbstractFlywayMigration.runFlyway(AbstractFlywayMigration.java:110)
    at io.micronaut.flyway.AbstractFlywayMigration.forceRun(AbstractFlywayMigration.java:93)
    at io.micronaut.flyway.AbstractFlywayMigration.run(AbstractFlywayMigration.java:66)
    at io.micronaut.flyway.DataSourceMigrationRunner.lambda$onCreated$1(DataSourceMigrationRunner.java:65)
    at java.base/java.util.Optional.ifPresent(Optional.java:178)
    at io.micronaut.flyway.DataSourceMigrationRunner.onCreated(DataSourceMigrationRunner.java:63)
    at io.micronaut.flyway.DataSourceMigrationRunner.onCreated(DataSourceMigrationRunner.java:39)
    at io.micronaut.context.DefaultBeanContext.triggerBeanCreatedEventListener(DefaultBeanContext.java:2387)
    at io.micronaut.context.DefaultBeanContext.postBeanCreated(DefaultBeanContext.java:2364)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2308)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2316)
    at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3127)
    at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
    at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:3029)
    at io.micronaut.context.DefaultBeanContext.initializeEagerBean(DefaultBeanContext.java:2693)
    at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1994)
    ... 8 more

I did change the container image with different version

test-resources.containers.postgres.image-name=postgres:10.3

This issue is related to the flyway migration, but not sure what going wrong


Solution

  • see: https://micronaut.io/launch?type=DEFAULT&name=demo&package=com.example&javaVersion=JDK_21&lang=JAVA&build=GRADLE_KOTLIN&test=JUNIT&features=flyway&features=postgres&features=data-jdbc

    try adding runtimeOnly("org.flywaydb:flyway-database-postgresql")

    addition to the base Flyway dependency that will be included automatically with the above, some databases require an additional database-specific Flyway library. Include one of the following dependencies as needed for your database:

    https://micronaut-projects.github.io/micronaut-flyway/7.6.0/guide/