You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Micronaut项目结合PostgreSQL、Flyway、JOOQ使用Test Resources报错求助

问题描述

我通过CLI创建了一个带有data、flyway、postgres、test-resources、jooq特性的Micronaut Java项目,命令如下:

mn create-app --lang java check-test-resources --features data,flyway,postgres,test-resources,jooq

以下是我的build.gradle配置:

plugins {
    id("com.github.johnrengelman.shadow") version "7.1.2"
    id("io.micronaut.application") version "3.7.0"
    id("io.micronaut.test-resources") version "3.7.0"
}

version = "0.1"
group = "check.test.resources"

repositories {
    mavenCentral()
}

dependencies {
    annotationProcessor("io.micronaut:micronaut-http-validation")
    implementation("io.micronaut:micronaut-http-client")
    implementation("io.micronaut:micronaut-jackson-databind")
    implementation("io.micronaut.flyway:micronaut-flyway")
    implementation("io.micronaut.sql:micronaut-jdbc-hikari")
    implementation("io.micronaut.sql:micronaut-jooq")
    implementation("jakarta.annotation:jakarta.annotation-api")
    runtimeOnly("ch.qos.logback:logback-classic")
    runtimeOnly("org.postgresql:postgresql")
    implementation("io.micronaut:micronaut-validation")

}


application {
    mainClass.set("check.test.resources.Application")
}
java {
    sourceCompatibility = JavaVersion.toVersion("17")
    targetCompatibility = JavaVersion.toVersion("17")
}

graalvmNative.toolchainDetection = false
micronaut {
    runtime("netty")
    testRuntime("junit5")
    processing {
        incremental(true)
        annotations("check.test.resources.*")
    }
}

我的application.yml配置如下:

micronaut:
  application:
    name: checkTestResources
datasources:
  default:
    driver-class-name: org.postgresql.Driver
    db-type: postgres
flyway:
  datasources:
    default:
      enabled: true
netty:
  default:
    allocator:
      max-order: 3

test-resources:
  containers:
    postgres:
      image-name: postgres:14.6

运行./gradlew test时,按文档预期Test Resources应该自动创建并使用PostgreSQL容器,但实际出现如下错误:

Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type  [javax.sql.DataSource]

Message: Error configuring data source 'default'. No URL specified
Path Taken: DataSource.dataSource(DatasourceConfiguration datasourceConfiguration) --> DataSource.dataSource([DatasourceConfiguration datasourceConfiguration])
io.micronaut.context.exceptions.BeanInstantiationException: Bean definition [javax.sql.DataSource] could not be loaded: Error instantiating bean of type  [javax.sql.DataSource]

请问我哪里配置错误?我希望在Micronaut项目中通过Test Resources使用Testcontainers。


解决方案

问题根源是数据源未配置Test Resources自动填充的标识,同时缺少必要的测试依赖支持,具体修复步骤如下:

  1. 修改数据源配置,启用Test Resources自动注入
    Micronaut Test Resources需要明确标识数据源需要被测试容器接管,修改application.yml中的数据源配置,添加test-resources.enabled: true:
datasources:
  default:
    driver-class-name: org.postgresql.Driver
    db-type: postgres
    test-resources:
      enabled: true

或者也可以用占位符方式,让Test Resources自动填充连接信息:

datasources:
  default:
    driver-class-name: org.postgresql.Driver
    db-type: postgres
    url: ${testcontainers.postgres.url}
    username: ${testcontainers.postgres.username}
    password: ${testcontainers.postgres.password}
  1. 补充测试依赖
    在build.gradle的dependencies块中添加Testcontainers PostgreSQL支持和Micronaut测试注解依赖:
testImplementation("io.micronaut.test:micronaut-test-junit5")
testImplementation("org.testcontainers:postgresql")
  1. 确保测试类使用正确注解
    测试类必须添加@MicronautTest注解,该注解会自动激活Test Resources功能,示例测试类:
package check.test.resources;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;

@MicronautTest
public class DataSourceTest {

    @Test
    void testDatabaseConnection() {
        // 此处可编写数据库相关测试逻辑,数据源已由Test Resources自动配置
    }
}

完成以上配置后,重新运行./gradlew test,Test Resources会自动拉起指定版本的PostgreSQL容器,并自动填充数据源的连接参数,即可解决报错问题。

内容的提问来源于stack exchange,提问作者Błażej Gruszka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 05:55:16