1---2name: junit5-core3description: Use when Codex needs to create, run, debug, evaluate, refactor, or correct JUnit 5 tests across component, integration, slice, repository, service, smoke, or regression scenarios.4---56# JUnit 5 Core78## 1. Preflight9101. Inspect the build file, Java version, source layout, and current JUnit dependencies.112. Inspect existing test conventions before adding new classes or annotations.123. Read [references/preflight.md](references/preflight.md) before major changes.134. Read [references/test-types.md](references/test-types.md) before choosing the test shape.145. Read [references/framework-recipes.md](references/framework-recipes.md) when the repo uses Spring Boot, Quarkus, Micronaut, Mockito, or Testcontainers.1516## 2. Choose the Test Shape17181. Use component tests for pure logic with in-memory collaborators.192. Use slice or repository tests when a framework boundary matters but the whole application does not.203. Use integration tests when multiple layers must collaborate realistically.214. Use service-isolated tests when the service boundary matters but downstream dependencies should stay controlled.225. Use service-end-to-end tests only when the real dependency chain is required by the risk.236. Use smoke tests for narrow health checks and regression tests for previously broken behavior.247. Read [references/service-test-strategies.md](references/service-test-strategies.md) before adding slow or infrastructure-heavy service coverage.2526## 3. Implement the Test27281. Prefer JUnit Jupiter annotations and assertions for new code.292. Prefer `@ParameterizedTest` over duplicated cases when the input matrix is systematic.303. Prefer soft assertions (`assertAll()`) to find and fix multiple errors in one test execution cycle.314. Use `@Nested` when scenario hierarchy clarifies behavior.325. Use test data builders, fakes, or fixtures to reduce setup noise.336. Keep one behavioral reason per test.347. Keep assertions semantic and stable.358. Read [references/junit5-authoring.md](references/junit5-authoring.md) for detailed authoring patterns.369. Read [references/lifecycle-tags-and-parallelism.md](references/lifecycle-tags-and-parallelism.md) before changing lifecycle, tags, ordering, or parallel execution.3710. Read [references/assertion-quality-and-smells.md](references/assertion-quality-and-smells.md) when the suite has weak, noisy, or brittle assertions.3839## 4. Run and Debug40411. Run the narrowest relevant test first.422. Reproduce failures before changing code.433. Distinguish between bad test, bad fixture, bad environment, and real product bug.444. Read [references/execution-and-debugging.md](references/execution-and-debugging.md) when triage is non-trivial.455. Read [references/error-index.md](references/error-index.md) when the failure pattern is common and recognizable.4647## 5. Correct Safely48491. Fix the smallest responsible layer first.502. Keep failing evidence visible until the issue is understood.513. Do not mute flaky behavior with sleeps or test ordering.524. Rerun narrowly after the fix, then rerun the affected suite slice.5354## 6. Examples55561. Input: `Write component tests for MoneyFormatter.`57 Output: Add a focused JUnit 5 class with semantic assertions and no framework bootstrapping.582. Input: `Fix this flaky repository test.`59 Output: Remove shared state, stabilize fixture setup, and rerun the narrow test target first.603. Input: `Add regression coverage for the null currency bug.`61 Output: Add a targeted regression test that reproduces the historical failure and verifies the corrected behavior.624. Input: `Cover this Spring repository query with the lightest useful test.`63 Output: Add a repository-focused slice or integration test aligned to the existing Spring test harness instead of booting the whole application without a reason.645. Input: `Turn these six input-output examples into JUnit 5 coverage.`65 Output: Use a parameterized test if the setup and assertion logic are shared.6667## 7. Troubleshooting68691. Problem: Tests pass only in suite order.70 Fix: Remove shared mutable state and reset fixtures per test.712. Problem: The test duplicates the same case with tiny input changes.72 Fix: Replace duplication with a parameterized test.733. Problem: The service test is slow and brittle.74 Fix: Reclassify it as slice, repository, or service-isolated when the risk allows it.754. Problem: The test suite becomes flaky only under parallel execution.76 Fix: Inspect shared fixtures, mutable statics, file-system reuse, and external ports before changing assertions.775. Problem: The test uses a full framework boot path for pure logic.78 Fix: Collapse it into a component test and keep only the framework-backed cases where the boundary matters.