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

Testcontainers中postgresqlContainer.withLogConsumer找不到符号的问题解决

问题:Testcontainers中Slf4jLogConsumer编译报错"cannot find symbol"

在Spring Boot 3.1.0项目中,使用Testcontainers初始化PostgreSQL容器时,执行以下代码出现编译错误:

postgresqlContainer.withLogConsumer(new Slf4jLogConsumer(log));

错误信息

/PostgresqlContainerInitializer.java:36: error: cannot find symbol
postgresqlContainer.withLogConsumer(new Slf4jLogConsumer(log));

相关代码与依赖

容器初始化类代码

@Slf4j
public class PostgresqlContainerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

   private static final String POSTGRESQL_IMAGE = "postgres:14-alpine";

    private static final String[] CUSTOM_COMMAND_PARTS = {"-c", "shared_buffers=256MB", "-c", "max_connections=300"};

    private static final PostgreSQLContainer<?> postgresqlContainer = new PostgreSQLContainer<>(
            DockerImageName.parse(POSTGRESQL_IMAGE)
                    .asCompatibleSubstituteFor("postgres")
    ).withDatabaseName("testdb");

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        postgresqlContainer.withLogConsumer(new Slf4jLogConsumer(log));
        postgresqlContainer.setCommand(ArrayUtils.addAll(postgresqlContainer.getCommandParts(), CUSTOM_COMMAND_PARTS));
        postgresqlContainer.start();

        ConnectionFactoryOptions options = PostgreSQLR2DBCDatabaseContainer.getOptions(
                postgresqlContainer
        );

        String r2dbcUrl = String.format("r2dbc:pool:postgresql://%s:%s/%s",
                options.getRequiredValue(HOST),
                options.getRequiredValue(PORT),
                options.getRequiredValue(DATABASE)
        );


        TestPropertyValues testPropertyValues = TestPropertyValues.of(
                "r2dbc.db.url=" + r2dbcUrl,
                "r2dbc.db.username=" + options.getRequiredValue(USER),
                "r2dbc.db.password=" + options.getRequiredValue(PASSWORD),
                "r2dbc.db.port="+ options.getRequiredValue(PORT),
                "r2dbc.db.db-name=" + options.getRequiredValue(DATABASE),
                "r2dbc.db.driver-name=" + options.getRequiredValue(DRIVER),
                "r2dbc.db.host=" + options.getRequiredValue(HOST)
        );

        testPropertyValues.applyTo(applicationContext.getEnvironment());
    }
}

build.gradle依赖配置

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.1.0'
    id 'io.spring.dependency-management' version '1.1.0'
}

....
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
.....

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
    implementation 'org.springframework.boot:spring-boot-starter-security'
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.springframework.boot:spring-boot-starter-webflux'
    implementation 'org.projectlombok:lombok'

    implementation 'org.flywaydb:flyway-core'
    implementation 'org.springframework:spring-jdbc'

    implementation 'org.thymeleaf.extras:thymeleaf-extras-springsecurity6'

    compileOnly 'org.projectlombok:lombok'

    runtimeOnly 'org.postgresql:postgresql'

    runtimeOnly 'org.postgresql:r2dbc-postgresql'

    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testImplementation 'org.springframework.boot:spring-boot-testcontainers'
    testImplementation 'io.projectreactor:reactor-test'
    testImplementation 'org.springframework.security:spring-security-test'
    testImplementation 'org.testcontainers:junit-jupiter'
    testImplementation 'org.testcontainers:postgresql'
    testImplementation 'org.testcontainers:r2dbc'
}

问题原因

  • Slf4jLogConsumer是Testcontainers的SLF4J日志适配类,不属于核心模块或postgresql模块,它单独放在testcontainers-slf4j模块中
  • 当前依赖仅引入了Testcontainers的基础和数据库相关模块,缺少slf4j适配模块,导致编译器找不到该类

解决办法

  1. 在build.gradle的testImplementation依赖中添加testcontainers-slf4j模块:
testImplementation 'org.testcontainers:slf4j'
  1. 确保代码中导入正确的包(如果IDE没有自动导入):
import org.testcontainers.containers.output.Slf4jLogConsumer;

内容的提问来源于stack exchange,提问作者skyho

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 23:07:47