跳到正文
Skillpedia
测试40k · #7378

quarkus-dev-services-and-testing

它能做什么

这个工具帮助你自动测试 Quarkus 代码。它会为测试启动所需的数据库和其他服务,无需手动配置。

适用场景

  • ✓为 Quarkus 项目编写测试时。
  • ✓需要为测试模拟数据库时。
  • ✓修复现有测试中的问题时。

技能链接

https://github.com/github/awesome-copilot/blob/main/skills/quarkus-dev-services-and-testing/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/github/awesome-copilot/blob/main/skills/quarkus-dev-services-and-testing/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“我想用 PostgreSQL 数据库测试我的用户 API。”

每周 5 个新技能,1 分钟读完。

工作原理

运行测试时,Quarkus 会检查是否需要数据库等服务。如果需要,但没有找到已配置的服务,它会自动使用 Docker 启动一个。这样,你的测试就有了运行所需的环境。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Quarkus Dev Services and Testing

Check the project's Quarkus version first. Framework-neutral Testcontainers usage can use the optional java-testcontainers skill if installed (otherwise see https://java.testcontainers.org/); this skill covers only Quarkus-specific features.

Dev Services

In dev and test modes Quarkus starts containers (PostgreSQL, MySQL, Kafka, Redis, Keycloak, ...) automatically when the related extension is present and no connection is configured (Docker or Podman required).

# Global switch
quarkus.devservices.enabled=true
%dev.quarkus.datasource.devservices.image-name=postgres:17
# Reuse container across restarts (also requires testcontainers.reuse.enable=true in ~/.testcontainers.properties)
quarkus.datasource.devservices.reuse=true
# Fixed port (optional)
quarkus.datasource.devservices.port=5432
# Share between apps in dev mode
quarkus.kafka.devservices.shared=true

Disable one with quarkus.<ext>.devservices.enabled=false; configuring a URL/host explicitly also disables it. Dev UI (/q/dev-ui) lists running services.

Tests

@QuarkusTest
class GreetingResourceTest {
    @Test
    void hello() {
        given().when().get("/hello").then().statusCode(200).body(is("Hello"));
    }
}

Dependencies, test scope: io.quarkus:quarkus-junit (3.31+, what the 3.40 template generates) or quarkus-junit5 (3.27 template; still published and resolvable on 3.40) and io.rest-assured:rest-assured. Keep whichever the project already uses.

  • Inject beans in tests with @Inject; mock CDI beans with io.quarkus.test.InjectMock (quarkus-junit-mockito, formerly quarkus-junit5-mockito; verified mocking a PanacheRepository bean on 3.40.1).
  • Security: @TestSecurity(user="alice", roles="admin") (quarkus-test-security).
  • Profiles: @TestProfile(MyProfile.class) with QuarkusTestProfile#getConfigOverrides(); restarts the app, so use sparingly.
  • Transactions: @TestTransaction rolls back after the test.
  • @QuarkusIntegrationTest runs against the packaged artifact (jar, native binary or container); you cannot inject beans there, only call it over HTTP.

Test resources

public class WireMockResource implements QuarkusTestResourceLifecycleManager {
    public Map<String,String> start() { /* start, return config overrides */ return Map.of(); }
    public void stop() { }
}

@QuarkusTest
@WithTestResource(WireMockResource.class)   // io.quarkus.test.common.WithTestResource
class MyTest { }

@WithTestResource (verified on 3.40.1) applies to the annotated test class; Quarkus restarts the app when the set of resources changes. The older @QuarkusTestResource still compiles and runs on 3.40.1 but applies globally to all tests unless restrictToAnnotatedClass = true. Do not put @ConfigProperty injection points for resource-supplied values in a test class: test classes are scanned as beans, and the app fails to start for other test classes that lack the resource. Prefer Dev Services over custom resources when an extension supports it.

Running

./mvnw test                 # unit/@QuarkusTest
./mvnw verify -DskipITs=false  # also runs *IT via maven-failsafe-plugin

Gotcha: *IT classes (@QuarkusIntegrationTest) are run by Failsafe in the verify phase, and need the quarkus-maven-plugin build goal and the failsafe config (with java.util.logging.manager and maven.home system properties) generated by the project template. ./mvnw test silently skips them, and the 3.27 and 3.40 templates also set <skipITs>true</skipITs>, so plain ./mvnw verify skips them too (verified): pass -DskipITs=false (or -Dnative, whose profile flips it). With -Dnative and a container build on macOS or Windows, the binary is a Linux ELF and the IT fails with Exit code is: '126'; use a local Mandrel/GraalVM build for those.

Continuous testing: run ./mvnw quarkus:dev and press r to start or re-run tests and p to pause them (h lists the hotkeys); once running, tests re-run on code changes (quarkus.test.continuous-testing=enabled|paused|disabled).

Verified on Quarkus 3.40.1 (JDK 26, Docker Desktop): @QuarkusTest with Dev Services PostgreSQL (postgres:18 by default), @TestTransaction, @TestProfile, @TestSecurity, @InjectMock, *IT via Failsafe. @QuarkusTest and Dev Services on 3.27.6 as well.

Checklist

  • Use Dev Services before writing a custom resource.

…

常见问题

我需要额外安装什么吗?

你需要在电脑上安装并运行 Docker 或 Podman,服务才能自动启动。

怎样关闭这个功能?

你可以通过修改 Quarkus 设置,为某一类服务(比如数据库)关闭自动启动,也可以为所有服务关闭。

这个功能可以用于其他类型的测试吗?

可以。这个工具主要针对 Quarkus 生态系统内的测试,因此很容易与该框架的特定功能集成。

详情

作者的原始描述

Quarkus Dev Services and testing: zero-config containers, @QuarkusTest, @QuarkusIntegrationTest, test resources, RestAssured, @InjectMock, profiles and continuous testing. Use when writing or fixing tests in a Quarkus project.

repo: github/awesome-copilot

热度

GitHub 星标
40k
排名
#7378
许可证
MIT
更新时间
2026/10/15

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能