AEM Test Gate: a Claude Code hook + skill that writes, runs and repairs JUnit tests
AI agents write AEM unit tests fast, but a test that was generated and never run only proves the model wrote something plausible, not that the code works. AEM Test Gate is a Claude Code hook + skill, committed to the repo, that turns "write me a test" into a gated, verified loop: 1. Detect: a hook notices when a new class lands under src/main/java with no matching *Test.java, and starts the skill. Nobody has to remember to ask. 2. Classify and draft: it identifies the class as a Sling Model, OSGi service or Sling servlet and drafts a JUnit 5 test with the right AEM Mocks scaffold. 3. Gate 1: it asks before saving the test. 4. Gate 2: it asks before running ./mvnw test. 5. Repair: on failure it fixes the test, never the production class, at most twice, and flags a likely production bug instead of weakening assertions. 6. Prove: it reports pass/fail and JaCoCo coverage for the class. Two Python scripts work without any agent: test_gap_audit.py lists untested classes in any AEM Maven project, and coverage_gate.py fails a build under a coverage threshold. I realized there is already some recipes around testing, went ahead and added hooks and skills to repair and identify missing tests and track coverage. [TRYABLE] Prerequisites: JDK 11+, Python 3, Claude Code (on Windows: Git for Windows). No Maven install needed; the repo pins Maven via the wrapper. 1. git clone https://github.com/snbaem/aem-test-gate.git 2. cd aem-test-gate 3. ./mvnw clean test (Windows: .\mvnw.cmd clean test) → expect 25 tests, BUILD SUCCESS 4. Open the folder in Claude Code, start a NEW session, and paste the prompt below. "Create a Sling Model MyTeaserModel in com.example.demo.models with optional title, description and linkUrl properties. Trim values, add .html to internal /content links, and add an isConfigured() that needs both a title and a link." 5. Watch for: the hook fires → class classified → draft test shown → asks to save → asks to run → test runs → pass/fail + coverage reported. Saying no at either gate stops it. 6. To use it on your own AEM project, copy .claude/ (hook + skill) and scripts/. [SETUP] Setup requirements - JDK 11+ (Java 8 fails with "invalid flag: --release") - Python 3 (standard library only) - Git and access to Maven Central (no Maven install needed; the wrapper pins 3.9.9) - Claude Code (on Windows, also Git for Windows) For your own AEM project: AEM Mocks + JUnit 5 + bnd-maven-plugin (the AEM Archetype has these), then copy .claude/ and scripts/. [IMPACT] One prompt produced a Sling Model plus 14 tests that passed first run at 100% line/branch coverage, no repairs. Proven Scalability. [LESSONS] Challenges around java set up - Claude does figure it out. Do not create model class suggested in readme file, that breaks the read me validation since the class it expects to be created is already there. The hook only sees files the agent writes. Run test_gap_audit.py (ideally in CI) for classes written by hand. [SAMPLE_OUTPUT] https://github.com/snbaem/aem-test-gate
