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

如何以编程方式在测试模式下为Quarkus指定运行端口

Quarkus + Concordion测试:编程指定端口/配置的解决方案

你的问题核心是:仅在RequestSpecification里设置了端口,但Quarkus启动流程未接收该配置,导致应用仍运行在默认端口——虽然能读取到预期的配置值,但实际并未生效。以下是两种可行的解决方式:

方式一:直接传入单个配置属性

修改startApplication方法,通过QuarkusBootstrap.builder().setConfig()直接指定端口属性,启动后同步更新RequestSpecification的端口:

private void startApplication() {
    try {
        PathsCollection.Builder rootBuilder = PathsCollection.builder();

        Path testClassLocation = PathTestHelper.getTestClassesLocation(getClass());
        rootBuilder.add(testClassLocation);

        final Path appClassLocation = PathTestHelper.getAppClassLocationForTestLocation(
                testClassLocation.toString());
        rootBuilder.add(appClassLocation);

        // 自定义端口,按需修改
        String targetPort = "8082";

        application = QuarkusBootstrap.builder()
                    .setIsolateDeployment(false)
                    .setMode(QuarkusBootstrap.Mode.TEST)                        
                    .setProjectRoot(Paths.get("").normalize().toAbsolutePath())
                    .setApplicationRoot(rootBuilder.build())
                    // 把端口配置传入Quarkus启动流程
                    .setConfig(Map.of("quarkus.http.port", targetPort))
                    .build()
                    .bootstrap()
                    .createAugmentor()
                    .createInitialRuntimeApplication()
                    .run();
        
        // 同步更新请求客户端的端口
        server = new RequestSpecBuilder()                                       
                .setPort(Integer.parseInt(targetPort))
                .setContentType(ContentType.JSON)
                .build();

    } catch (BindException e) {             
        e.printStackTrace();
        System.out.println("Address already in use - which is fine!");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

方式二:加载自定义配置文件

如果需要配置多个参数,推荐使用自定义配置文件。比如在src/test/resources下创建application-concordion-test.properties,然后在启动时加载:

  1. 配置文件内容:
quarkus.http.port=8082
# 可添加其他测试需要的配置项
  1. 修改启动代码:
private void startApplication() {
    try {
        PathsCollection.Builder rootBuilder = PathsCollection.builder();

        Path testClassLocation = PathTestHelper.getTestClassesLocation(getClass());
        rootBuilder.add(testClassLocation);

        final Path appClassLocation = PathTestHelper.getAppClassLocationForTestLocation(
                testClassLocation.toString());
        rootBuilder.add(appClassLocation);

        // 指定自定义配置文件路径
        Path customConfigFile = Paths.get("src/test/resources/application-concordion-test.properties").toAbsolutePath();

        application = QuarkusBootstrap.builder()
                    .setIsolateDeployment(false)
                    .setMode(QuarkusBootstrap.Mode.TEST)                        
                    .setProjectRoot(Paths.get("").normalize().toAbsolutePath())
                    .setApplicationRoot(rootBuilder.build())
                    // 加载自定义配置文件
                    .addConfigFile(customConfigFile)
                    .build()
                    .bootstrap()
                    .createAugmentor()
                    .createInitialRuntimeApplication()
                    .run();
        
        // 从应用配置中读取实际端口,更新请求客户端
        String actualPort = application.getConfigValue("quarkus.http.port", String.class);
        server = new RequestSpecBuilder()                                       
                .setPort(Integer.parseInt(actualPort))
                .setContentType(ContentType.JSON)
                .build();

    } catch (BindException e) {             
        e.printStackTrace();
        System.out.println("Address already in use - which is fine!");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

关键注意事项

  • 必须通过QuarkusBootstrap的API传递配置,而非仅修改RequestSpecification,这样Quarkus启动时才会真正应用配置。
  • 启动后要同步更新RequestSpecification的端口,确保请求发送到正确的地址。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 20:11:47