Skip to content

skillTestingjava

JUnit Testing Java

Writes JUnit 5 tests with parameterized cases and Mockito used carefully, avoiding static mocking, over-stubbing and tests tied to implementation.

npx workspacetemplates add junit-testing-java --tool claude-code

Writes 1 file to .claude/skills/junit-testing-java/

.claude/skills/junit-testing-java/SKILL.md28 lines · 1.8 KBFile
---
name: junit-testing-java
description: Writes JUnit 5 tests with parameterized cases and Mockito used carefully, avoiding static mocking, over-stubbing and tests tied to implementation.
---

# JUnit 5 and Mockito testing

Test behavior through the public API. Mock only what is slow, external or nondeterministic.

## Steps

1. Use JUnit 5 (`org.junit.jupiter`). One class per unit under test, named `OrderServiceTest`, tests named for behavior: `rejectsOrderWithoutItems()`. Use `@Nested` to group by scenario and `@DisplayName` where the name needs prose.
2. Build objects with small test builders or factory methods. Prefer constructor injection so the test needs no reflection or Spring context. Use `@SpringBootTest` only for wiring tests, and slice annotations (`@DataJpaTest`, `@WebMvcTest`) for the rest.
3. Cover many cases with `@ParameterizedTest` and `@CsvSource`, `@MethodSource` or `@EnumSource`. Give cases names with `name = "{index}: {0}"`.
4. Assertions: AssertJ (`assertThat(result).isEqualTo(...)`) when available. Use `assertThrows` and check the message or type. Group related checks with `assertAll`.
5. Mockito: `@ExtendWith(MockitoExtension.class)` for strict stubs, `@Mock` for outside collaborators, `verify` only when the call is the outcome (sent email, published event). Use `ArgumentCaptor` to inspect arguments.
6. Replace time and randomness with an injected `Clock` or `Supplier`.

## Rules

- Never use `mockStatic` or `mockConstruct` to dodge a design problem; wrap the static call in an interface and inject it.
- Never mock value objects, collections or the class under test.
- No `Thread.sleep`; use Awaitility.
- Remove unnecessary stubbings that strict mode reports.

## Report

Tests added, any design change suggested to avoid static mocking, and the result of `mvn test` or `gradle test`.

What it does

Java tests drift toward Mockito-heavy code that restates the implementation line by line. This skill shows the agent how to write JUnit 5 tests that survive refactoring: real collaborators where cheap, mocks at the edges, and clear assertions.

It also flags the usual traps, including static mocking, strict stubbing warnings, and tests that only verify interactions.

Example

you › Write tests for DiscountService and remove the mockStatic usage

agent › Wrapped LocalDate.now() in an injected Clock and replaced mockStatic with Clock.fixed. Added 12 parameterized cases for tier and date combinations, 3 nested scenario groups, and an assertThrows for negative totals. Removed 4 unused stubs flagged by strict mode. gradle test: 27 passed.

Questions

Should I still use @SpringBootTest for service tests?
Rarely. It starts the whole context and slows the suite. Construct the service directly and keep one or two context tests for wiring.
Why avoid mockStatic?
It is global, thread-sensitive and ties the test to a hidden dependency. Injecting a small interface or Clock makes the dependency visible and the test simpler.
  • Contract Testing

    SkillTesting

    Free
  • E2E Test Design

    SkillTesting

    Free
  • Flaky Test Diagnosis

    SkillTesting

    Free