Skip to content
Back to skills

Junit Testing Java

ASecurity

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

  • 3 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added November 15, 2026
ai-agentsjavaspringtestingapi

Works with

  • api

Security analysis

A100/100

Scanned November 15, 2026

npx -y skills add workspace-templates/ai-agent-templates --skill junit-testing-java --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Junit Testing Java?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Junit Testing Java
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/workspace-templates-junit-testing-java/badge)](https://www.skillsdirectory.com/skills/workspace-templates-junit-testing-java)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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`.

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.