You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Quarkus集成测试中Microcks无法访问应用的问题求助

Quarkus集成测试中Microcks无法访问应用的问题求助

大家好,我最近基于OpenAPI规范用Quarkus开发了一个REST API项目,想通过Microcks做契约测试。单元测试跑起来完全没问题,但一跑集成测试就遇到了Microcks容器无法访问我的Quarkus测试应用的问题,想请教下各位大佬怎么解决~

项目背景

先简单说下我的项目情况:

  • 基于OpenAPI 3.0定义了SampleAPI,核心接口是GET /sender用于返回Sender列表
  • 实现了SenderResource类来处理接口逻辑,用内存缓存模拟数据库存储数据
  • 配置了Quarkus Microcks的DevServices,也添加了测试依赖
  • 单元测试能正常通过,但集成测试直接卡壳

OpenAPI 规范片段

openapi: 3.0.0
x-stoplight:
  id: 1q8257l49074g
info:
  title: SampleAPI
  version: '1.0'
  description: |-
    A sample API demonstrating OpenAPI 3.0 and Microcks integration.
  license:
    name: Apache 2.0
paths:
  /sender:
    get:
      tags:
        - Sender
      summary: Get All Senders
      description: Retrieves all sender entries.
      operationId: getAllSenders
      responses:
        '200':
          description: Successful response
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/SenderResult'
              examples:
                all:
                  value:
                    - id: 1
                      message: "Hello Receiver"
                      status: "sent"
                    - id: 2
                      message: "Another message"
                      status: "pending"
        '500':
          description: Internal server error
          content:
            text/plain:
              schema:
                type: string
                example: "Internal Server Error"
components:
  schemas:
    SenderResult:
      type: object
      required:
        - id
        - message
        - status
      properties:
        id:
          type: integer
        message:
          type: string
        status:
          type: string

资源类代码

@Slf4j
@ApplicationScoped
public class SenderResource implements SenderApi {

    private final ReceiverClient receiverClient;
    // 用内存缓存模拟数据库
    private final ConcurrentHashMap<Integer, SenderResult> senderCache = new ConcurrentHashMap<>();

    @Inject
    public SenderResource(@RestClient ReceiverClient receiverClient) {
        this.receiverClient = receiverClient;
        log.info("Initializing SenderResource...");

        // 默认测试数据
        SenderResult defaultSender = new SenderResult()
                .id(1)
                .message("default")
                .status("success");
        senderCache.put(defaultSender.getId(), defaultSender);

        // 另一条测试数据
        SenderResult anotherSender = new SenderResult()
                .id(2)
                .message("another")
                .status("QUEUED");
        senderCache.put(anotherSender.getId(), anotherSender);
    }

    /**
     * 获取所有Sender数据
     */
    @Override
    public Response getAllSenders() {
        return Response.ok(senderCache.values()).build();
    }
}

Microcks配置

application.yaml中的配置:

quarkus:
  microcks:
    devservices:
      enabled: true

Maven依赖配置:

<dependency>
  <groupId>io.github.microcks.quarkus</groupId>
  <artifactId>quarkus-microcks</artifactId>
  <version>0.4.1</version>
  <scope>test</scope>
</dependency>

契约测试代码

@QuarkusTest
@Slf4j
public class SenderContractTest {

    static String microcksUrl;
    static Integer port;

    @BeforeAll
    static void init() {
        Config config = ConfigProvider.getConfig();
        microcksUrl = config
                .getOptionalValue("quarkus.microcks.default.http", String.class)
                .orElse("http://localhost:8085");
        port = config.getOptionalValue("quarkus.http.test-port", Integer.class)
                .orElse(8282);

        log.info("Microcks URL for contract tests: {}", microcksUrl);
        log.info("Test application port: {}", port);
    }

    @Test
    void testAllOperationsFromSenderOpenApi() throws Exception {
        String endpointUrl = "http://host.containers.internal:" + port;
        log.info("Endpoint URL for contract test: {}", endpointUrl);

        TestRequest request = new TestRequest.Builder()
                .serviceId("SenderAPI:1.0")
                .runnerType(TestRunnerType.OPEN_API_SCHEMA)
                .testEndpoint(endpointUrl)
                .build();

        TestResult result = MicrocksContainer.testEndpoint(microcksUrl, request);
        assertNotNull(result, "Microcks应该返回测试结果");
        logTestResultDetails(result);

        assertTrue(result.isSuccess(), "契约测试失败,查看上方日志获取详情");
        assertEquals(5, result.getTestCaseResults().size(), "测试用例数量应该和接口操作数匹配(GET all、GET by ID、POST、DELETE)");
    }
}

问题现象

单元测试能正常通过,但运行集成测试时,日志里只打印了Docker相关的初始化信息,看起来Microcks容器根本连不上我的Quarkus测试应用:

Server Version: 5.2.5
API Version: 1.41
Operating System: Fedora
Total Memory: 7358 MB
2025-10-08 17:24:34,176 WARN [org.tes.uti.ResourceReaper] (build-9) ********************************************************************************
Ryuk has been disabled. This can cause unexpected behavior in your environment.


2025-10-08 17:24:34,177 INFO [org.tes.DockerClientFactory] (build-9) Checking the system...
2025-10-08 17:24:34,178 INFO [org.tes.DockerClientFactory] (build-9) ✔︎ Docker server version should be at least 1.6.0
2025-10-08 17:24:34,181 INFO [org.tes.ima.PullPolicy] (build-9) Image pull policy will be performed by: DefaultPullPolicy()
2025-10-08 17:24:34,182 INFO [org.tes.uti.ImageNameSubstitutor] (build-9) Using DefaultImageNameSubstitutor
2025-10-08 17:24:34,370 INFO [tc.tes.2.0] (build-9) Creating container for image: testcontainers/sshd:1.2.0

我怀疑是容器间网络的问题,比如host.containers.internal在Fedora上可能不生效?或者有没有其他配置我漏掉了?麻烦各位帮忙看看怎么解决,谢谢啦!

火山引擎 最新活动