unit-testing

Enforces JUnit 5, Mockito, and AssertJ conventions for writing behavior-focused Java unit tests.

Updated Aug 22, 2026
One-click install
npx skills add https://github.com/balajirags/aifsd-kit --skill unit-testing-balajirags
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: unit-testing
Source: https://github.com/balajirags/aifsd-kit/tree/main/docs/skills/unit-testing
Command: npx skills add https://github.com/balajirags/aifsd-kit --skill unit-testing-balajirags

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Java test suites often degrade into bloated, flaky, or meaningless tests that mock the wrong things, assert multiple behaviors at once, or pad coverage numbers by testing trivial DTOs. This Skill gives an AI agent a strict, opinionated rulebook so every generated unit test is isolated, readable, and actually signals what broke. ## Core Features & Use Cases - Behavior-focused test structure: Enforces one behavior per test with should<Behavior>_when<Condition>() naming, Given/When/Then bodies, and @Nested grouping by SuccessCases, FailureCases, EdgeCases, and Validation. - Correct mocking discipline: Mandates @ExtendWith(MockitoExtension.class) with @Mock/@InjectMocks, mocking only external dependencies and never the class under test, plus verify(...) for required side-effect interactions. - Honest coverage policy: Skips tests for behavior-free classes (DTO records, trivial entities, @Configuration classes) and excludes only those packages from JaCoCo, never excluding real business logic to hit a target. - Use Case: Ask the agent to write unit tests for a new OrderService; it reads the whole class first, enumerates branches and error paths, and produces a fast, fully mocked suite with AssertJ assertions and no database or network calls. ## Quick Start Write unit tests for the OrderService class following the unit-testing conventions, covering success, failure, and edge cases with mocked dependencies.

Frequently Asked Questions about unit-testing

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write unit tests with JUnit 5 and Mockito?

Annotate the test class with @ExtendWith(MockitoExtension.class), declare dependencies as @Mock fields, and inject them into the class under test with @InjectMocks. Structure each test as Given/When/Then, stub with when(...).thenReturn(...), assert with AssertJ, and verify required interactions with verify(mock).method(...).

How should I name and organize unit test methods in Java?

Name each test should<Behavior>_when<Condition>() so it describes exactly one behavior, and group tests into @Nested classes such as SuccessCases, FailureCases, EdgeCases, and Validation. A failing suite then immediately signals which category of behavior broke.

Should I mock the class under test in Mockito?

No. Mock only external dependencies of the class under test, never the class itself. Spying on or mocking the service being tested defeats the purpose of the test and hides real behavior; use @InjectMocks so real logic runs against mocked collaborators.

Which classes should be excluded from JaCoCo coverage?

Exclude only genuinely behavior-free classes such as DTO records, JPA entities with only getters and setters, trivial enums, and @Configuration classes with only @Bean declarations, typically at the package level. Never exclude services, controllers, or repositories with real logic just to hit a coverage target.

When should I not write a unit test for a class?

Skip unit tests for classes with zero conditionals, branches, or custom logic, such as plain DTO records or trivial configuration classes, since they add noise without signal. If such a class later gains validation or behavior, it earns a test at that point.

Can unit tests make real database or network calls?

No. Unit tests must stay fast and isolated, so any real database or network call belongs in integration tests using Testcontainers or @SpringBootTest instead. A unit test that hits external systems becomes flaky and slow and stops being a unit test.