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

Quarkus测试:如何基于测试配置文件条件启用QuarkusTestResource

问题背景

我们有两个自定义测试配置文件:test-fast(本地测试用,主打速度)和test-slow(无需Docker环境)。当前Kafka相关测试的配置如下:

# Pipeline sets -Dquarkus.test.profile=test,test-slow
quarkus.test.profile=test,test-fast 
%test-fast.quarkus.kafka.devservices.enabled=false
%test-slow.quarkus.kafka.devservices.enabled=true

目前靠@BeforeAll里的assumeTrue,在test-fast环境下跳过Kafka测试:

@BeforeAll
static void beforeAll() {
    assumeTrue(ConfigUtils.getProfiles().contains("test-slow"), "Kafka is only set-up in the slow test profile");
}

现在要引入Kafka Companion验证消息,得用@QuarkusTestResource(KafkaCompanionResource.class)注解,但这个注解会直接启动Kafka实例——我们希望只在test-slow配置下启动它,同时也不清楚怎么通过%test-slow全局启用对应的测试配置。


方案1:用自定义测试Profile控制资源加载

1. 编写自定义测试Profile类

实现QuarkusTestProfile,仅在test-slow激活时注册KafkaCompanionResource:

import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.kafka.KafkaCompanionResource;
import java.util.List;
import java.util.Map;

public class SlowTestProfile implements QuarkusTestProfile {

    @Override
    public List<Class<? extends QuarkusTestResourceLifecycleManager>> testResources() {
        // 仅test-slow激活时加载KafkaCompanionResource
        return List.of(KafkaCompanionResource.class);
    }

    @Override
    public boolean isEnabled() {
        // 检查当前是否激活了test-slow配置
        return ConfigUtils.getProfiles().contains("test-slow");
    }

    @Override
    public Map<String, String> getConfigOverrides() {
        // 这里可以添加test-slow专属配置,比如强制开启Kafka DevServices
        return Map.of("quarkus.kafka.devservices.enabled", "true");
    }
}

2. 全局关联test-slow和自定义Profile

在application.properties中添加配置,让test-slow激活时自动应用该Profile:

%test-slow.quarkus.test.profile=SlowTestProfile

3. 调整测试类

测试类无需直接添加@QuarkusTestResource,由Profile自动控制资源加载:

import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

@QuarkusTest
public class KafkaRelatedTest {

    @BeforeAll
    static void beforeAll() {
        assumeTrue(ConfigUtils.getProfiles().contains("test-slow"), "Kafka is only set-up in the slow test profile");
    }

    // 你的Kafka测试方法...
}

方案2:用配置开关直接控制Kafka Companion

Kafka Companion本身支持配置开关,无需自定义Profile,更省心:

1. 修改application.properties配置

针对两个测试配置分别设置启用/禁用开关:

# test-fast下禁用Kafka Companion
%test-fast.quarkus.test.kafka-companion.enabled=false
# test-slow下启用Kafka Companion
%test-slow.quarkus.test.kafka-companion.enabled=true

2. 测试类保留注解

直接在测试类上添加@QuarkusTestResource,配置会自动控制它是否启动:

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.kafka.KafkaCompanionResource;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

@QuarkusTest
@QuarkusTestResource(KafkaCompanionResource.class)
public class KafkaRelatedTest {

    @BeforeAll
    static void beforeAll() {
        assumeTrue(ConfigUtils.getProfiles().contains("test-slow"), "Kafka is only set-up in the slow test profile");
    }

    // 你的Kafka测试方法...
}

这个方案更简洁,推荐优先使用。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 09:35:21