java-junit

软件研发 测试与质量 3 文件 EN 原文

'Write, review and fix Java unit tests with JUni

'Write, review and fix Java unit tests with JUnit 5 and JUnit 6 (Jupiter), AssertJ and Mockito: assertions, parameterized and nested tests, mocking rules, deterministic time and randomness, Surefire vs Failsafe, JaCoCo coverage, optional PIT mutation testing, and guardrails for agents (never skip or delete failing tests). Use when adding, refactoring or debugging tests in Maven or Gradle projects. Framework-neutral.'

说明语言:英文

来源githubawesome-copilot ↗ 查看原文件 ↗版权归原作者,请遵循原仓库许可
安装
npx skills add https://github.com/github/awesome-copilot --skill java-junit

需要已安装 skills CLI。也可以直接从源仓库 clone。

在星云中查看 ↗
SKILL.md 原文

name: java-junit description: 'Write, review and fix Java unit tests with JUnit 5 and JUnit 6 (Jupiter), AssertJ and Mockito: assertions, parameterized and nested tests, mocking rules, deterministic time and randomness, Surefire vs Failsafe, JaCoCo coverage, optional PIT mutation testing, and guardrails for agents (never skip or delete failing tests). Use when adding, refactoring or debugging tests in Maven or Gradle projects. Framework-neutral.' ---

Java Testing with JUnit 5/6, AssertJ and Mockito

Framework-neutral guidance for fast, deterministic unit tests. For Spring slices use spring-boot-testing (if installed); for real databases/brokers in Docker use java-testcontainers; for failures you cannot explain, java-debugging.

First: detect, don't impose

  1. Read pom.xml/build.gradle*: JUnit version (junit-jupiter, junit-bom, or inherited from a Spring Boot BOM), assertion library, Mockito, Surefire/Failsafe versions, Java release.
  2. Copy the conventions already in src/test/java (naming, assertion library, static imports, mocking style). Do not introduce AssertJ, Mockito or JaCoCo into a project that lacks them without asking.
  3. Never bump the JUnit major version implicitly; upgrading is its own task (see java-upgrade if installed).

JUnit 5 vs JUnit 6

JUnit 5 (last 5.14.x)JUnit 6 (6.0 released; 6.1.x current)
Java baseline817 (verified in the published Gradle module metadata)
VersioningPlatform 1.x, Jupiter 5.x, Vintage 5.xOne version number for Platform, Jupiter and Vintage
APIorg.junit.jupiter.api.*Same packages; the Jupiter tests in this skill compile and pass unchanged on 5.14.4 and 6.0.3
Removed in 6.0MethodOrderer.Alphanumeric, the deprecated two-argument InvocationInterceptor.interceptDynamicTest(Invocation, ExtensionContext) overload (the three-argument one remains), junit.jupiter.tempdir.scope, @CsvFileSource(lineSeparator)
CSVunivocity-parsersFastCSV: stricter parsing (no characters after a closing quote); header-field options now apply to headers
Otherjunit-jupiter-migrationsupport deprecated; invalid enum-style config values now fail discovery; @Nested class order deterministic but non-obvious

Source: JUnit 6.0.0 release notes (<https://docs.junit.org/6.0.0/release-notes.html>). Read the notes for the target version (<https://docs.junit.org/current/release-notes/>) and prefer them over this table. JUnit 4 tests need the Vintage engine; do not mix org.junit.Test and org.junit.jupiter.api.Test in one class.

Dependencies (same for 5 and 6; verified with Maven and Gradle): import org.junit:junit-bom, then declare org.junit.jupiter:junit-jupiter (aggregates api, params, engine) with test scope. Gradle 9 additionally requires testRuntimeOnly("org.junit.platform:junit-platform-launcher") (verified: without it the test executor fails with "Failed to load JUnit Platform") and tasks.test { useJUnitPlatform() }. Maven needs Surefire 3.x (verified with 3.6.0).

Assertions

Match the project's library. Plain Jupiter rules: expected value first, lazy Supplier messages, assertAll, assertThrowsExactly, timeouts. (The separate instructions/java-junit5-assertions.instructions.md resource has the longer version but is not installed with this skill.) If the project uses AssertJ, prefer it for collections, strings and exceptions; patterns (soft assertions, assertThatThrownBy, extracting, containsExactly, usingRecursiveComparison) are in references/assertj-and-mockito.md.

Mocking (Mockito)

Determinism

Unit vs integration tests

Unit (*Test)Integration (*IT)
MavenSurefire, mvn testFailsafe, mvn verify
Gradletest taskseparate integrationTest task (JVM Test Suite) wired into check
Speedmilliseconds, no I/Omay use Docker/DB

Surefire defaults pick Test, Test, Tests, TestCase; Failsafe picks IT, IT, *ITCase. Failsafe needs the integration-test and verify goals bound and is run by mvn verify, not mvn test. Alternatively split by @Tag. For databases and brokers see java-testcontainers; for Spring slices see spring-boot-testing. Full build snippets: references/build-setup.md.

Coverage and mutation testing (optional)

Agent guardrails

  1. Never delete, comment out, @Disabled, weaken (looser assertion, wider tolerance, swallowed exception) or skip a failing test to get green. Find the cause.
  2. Decide which side is wrong, the test or the production code. If the fix changes production behavior, stop and ask the user first; if the test encodes an obviously stale expectation, say so and propose the change.
  3. Do not add -DskipTests, -Dmaven.test.skip, -x test or failIfNoTests=false to make a build pass.
  4. Do not mock the class under test or assert on mock internals to "prove" correctness.
  5. Run the narrowest test first, then the full build:
  6. Maven: mvn -q -Dtest=OrdersTest test (one method: -Dtest='OrdersTest#place_should_saveAndNotify_when_itemValid'), then mvn verify.
  7. Gradle: ./gradlew test --tests 'demo.OrdersTest', then ./gradlew check. Prefer the project wrapper (./mvnw, ./gradlew) when present.
  8. Flaky test: reproduce by rerunning, then remove the nondeterminism (time, order, shared state, async waits); do not add retries.
  9. New code gets new tests beside the existing ones, in the same style: happy path, boundaries, error cases.

References

同类技能
apple-appstore-reviewerarize-ai-provider-integrationbacktesting-frameworksbats-testing-patternsbreakdown-testcode-review-excellencecsharp-mstestcsharp-nunit
本页内容来自开源项目 github, 版权归原作者所有。
本站仅作技能索引与分享,所有 Skill 版权归其原作者所有。