1---2name: oc-java-test3description: "Trigger: Java tests, JUnit, Mockito, AssertJ, Java legacy tests, Java characterization tests, Java seams. Generate and retrofit Java tests safely; planning the scope and protection of a legacy change belongs to oc-legacy."4license: MIT5metadata:6 source: https://skillmd.com/skills/andresnator/oc-java-test7 author: andresnator8 status: testing9 version: "4.1.1"10---1112# Java Testing Skill1314## Intent1516- **Goal:** Generate or retrofit Java tests safely: detect the local stack, choose the smallest safe technique, create or propose tests, and explain seam/dependency tradeoffs.17- **Limits:**18 - The caller owns product behavior, dependency approval, and running project commands when validation exists.19 - Preserve the project test stack. Do not add or upgrade JUnit, Mockito, AssertJ, PowerMock, or ApprovalTests unless the existing stack cannot support the required test and the tradeoff is stated.20 - Do not modify production behavior before a safety test exists for legacy code (Cover → Modify → Refactor).21- **Fails if:** Generated examples are incomplete, do not compile, or ignore local package, naming, and style conventions; collaborators are over-verified or over-mocked; a dependency-breaking seam is not named as Sensing or Separation.2223## Context2425- **Reads:** Java level, build tool, JUnit/Mockito versions, assertion style, and dependency management (detected independently); existing test naming, package layout, assertions, fixtures, and mocking style; class under test, observable behavior, collaborators, and failure/edge cases; for legacy code, change point, test point, hidden dependencies, and seam purpose. Inspect or ask before writing tests.26- **Never reads:** The full reference catalog by default; load progressively.27- **Persona:** Java test engineer following *Working Effectively with Legacy Code*.28- **Scope:** Java unit tests, JUnit/Mockito/AssertJ examples, characterization and Golden Master tests, seams, dependency-breaking. Not for non-Java projects, generic strategy, frontend/E2E, performance, security, or refactoring without a testing objective.2930## Expectations3132- **Scenarios:**33 - WHEN a clean, injectable class needs tests THEN write simple unit tests: pure unit first, Mockito only across real boundaries, Spring/context tests only when framework behavior is the subject; test behavior.34 - WHEN existing code has no tests but is easy to instantiate THEN characterize first, then improve.35 - WHEN legacy code is hard to test THEN follow the legacy flow and the decision table below.36- **Done means:** Return the chosen route and references used; detected Java test stack and conventions; files changed or proposed; tests added with behaviors and edge cases covered; dependency/seam tradeoffs, including Sensing vs Separation when applicable; validation performed or recommended command.37- **Quality needs:** Follow `oc-conventions` as the general contract; the Java specifics are below.3839## Method4041### Decision table4243| Situation | Route |44|---|---|45| Clean, injectable class needs tests | Simple unit tests below. |46| Existing code has no tests but is easy to instantiate | Characterization tests first, then improve. |47| Constructor/static/global/framework dependencies block testing | `references/quick-decision-flow.md` and dependency-breaking references. |48| Dependency setup is missing or unclear | `references/dependency-setup.md` before adding imports or build snippets. |49| Golden Master or ApprovalTests is needed | `references/characterization-tests.md`, then `references/approvaltests-setup.md` before creating baselines. |50| Static/final mocking or captors are needed | `references/mockito-patterns.md` before choosing Mockito features. |51| Large legacy cluster or unclear effect propagation | `references/advanced-patterns.md` for pinch points, effect sketches, and hot spots. |5253### Simple unit tests5455- Place tests under `src/test/java/` mirroring the source package.56- Name classes `{ClassName}Test` and methods `should{Behavior}When{Condition}` unless the project uses another convention.57- Split every non-trivial test body with `// Given`, `// When`, `// Then`; avoid other comments/Javadocs.58- For JUnit 5 + Mockito, use `@ExtendWith(MockitoExtension.class)`, `@Mock`, and `@InjectMocks` only when constructor setup adds noise.59- Prefer AssertJ `assertThat()` / `assertThatThrownBy()` when present; `WithAssertions` is acceptable if it matches project style.60- For complex objects/DTOs, assert the whole object with `usingRecursiveComparison()` or ApprovalTests instead of field-by-field cascades.61- Use parameterized tests for meaningful input matrices, and `ArgumentCaptor` only when the passed object is the behavior under test.6263### Legacy flow64651. Identify change points.662. Find test points with effect sketches.673. Break dependencies only where needed for Sensing or Separation.684. Write characterization tests documenting current behavior in a dedicated permanent `{ClassName}CharacterizationTest` class, separate from intent-revealing unit tests.695. Modify and refactor behind the safety net.7071### Resources7273Use `references/` for when and why to apply a technique; use `assets/examples/` only as copyable starting points, adapting package names, dependencies, naming, and conventions.7475| Resource | Load when |76|---|---|77| `references/dependency-setup.md` | Dependency setup or version compatibility is unclear. |78| `references/mockito-patterns.md` | Mockito annotations, captors, verification, static/final mocking, or interaction boundaries are needed. |79| `references/characterization-tests.md` | Existing behavior must be locked before modification, including Golden Master tests. |80| `references/approvaltests-setup.md` | ApprovalTests setup or baseline workflow is needed. |81| `references/seam-model.md` | Seams and enabling points must be classified before breaking dependencies. |82| `references/dependency-breaking.md` | Constructor, interface, override, parameter adaptation, or global-reference techniques are needed. |83| `references/quick-decision-flow.md` | The code is hard to test and you need a first route. |84| `references/sensing-separation.md` | Decide whether the seam observes behavior or separates dependencies. |85| `references/pass-null-subclass-override.md` | Pass Null, null object, subclass-and-override, or extract-and-override is the likely seam. |86| `references/sprout-wrap-techniques.md` | Risky legacy additions call for Sprout Method/Class or Wrap Method/Class. |87| `references/method-object-skin-wrap.md` | Long methods, API skin wrapping, or method-object extraction is needed. |88| `references/tdd-legacy.md` | The task asks for TDD while modifying existing legacy Java. |89| `references/advanced-patterns.md` | Pinch points, effect sketches, God classes, hot spots, or scratch refactoring are needed. |90| `assets/examples/ExampleServiceTest.java` | Service test template with JUnit 5, Mockito, and AssertJ. |91| `assets/examples/ExampleConfigTest.java` | Configuration/value-object style example. |92| `assets/examples/ExampleHandlerTest.java` | Handler/controller collaborator example. |93| `assets/examples/ExampleCacheTest.java` | Cache/stateful behavior example. |94| `assets/examples/ExampleListenerTest.java` | Listener/event-driven example. |