java-junit

Write JUnit 5 unit tests with parameterized tests, assertions, and Mockito mocking.

Updated Dec 26, 2025
One-click install
npx skills add https://github.com/jwvdvuurst/CalculatorCollection --skill java-junit-jwvdvuurst
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: java-junit
Source: https://github.com/jwvdvuurst/CalculatorCollection/tree/main/skills/java-junit
Command: npx skills add https://github.com/jwvdvuurst/CalculatorCollection --skill java-junit-jwvdvuurst

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing consistent, maintainable unit tests in Java requires knowing JUnit 5 conventions, parameterized test sources, assertion patterns, and mocking setup, which developers often get wrong or apply inconsistently. ## Core Features & Use Cases - Test Structure Guidance: Enforces AAA pattern, naming conventions, lifecycle annotations like @BeforeEach, and @DisplayName usage. - Data-Driven Testing: Covers @ParameterizedTest with @ValueSource, @MethodSource, @CsvSource, @CsvFileSource, and @EnumSource. - Assertions & Mocking: Recommends Assertions static methods, AssertJ fluent assertions, assertThrows, assertAll, and Mockito with @Mock and @InjectMocks. - Use Case: When adding tests for a new Calculator service class, generate a CalculatorTest with parameterized cases for edge inputs and mocked dependencies. ## Quick Start Write JUnit 5 tests for my Calculator class including parameterized tests for the divide method and a Mockito mock for its repository dependency.

Frequently Asked Questions about java-junit

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

FAQPage Schema
How do I write parameterized tests in JUnit 5?

Use @ParameterizedTest with a source annotation: @ValueSource for literals, @CsvSource for inline comma-separated values, @MethodSource for a factory method returning a Stream, or @CsvFileSource for classpath CSV files. Add junit-jupiter-params to your dependencies.

How to mock dependencies in JUnit 5 with Mockito?

Annotate dependencies with @Mock and the class under test with @InjectMocks so Mockito creates and injects mocks automatically. Coding to interfaces makes mocking easier and keeps tests isolated from real implementations.

What is the difference between @BeforeEach and @BeforeAll in JUnit 5?

@BeforeEach runs before every test method for per-test setup, while @BeforeAll runs once per class and must be a static method. Use @AfterEach and @AfterAll for the corresponding teardown logic.

Should I use JUnit assertions or AssertJ?

JUnit's org.junit.jupiter.api.Assertions covers standard checks like assertEquals, assertThrows, and assertAll. AssertJ provides fluent, more readable assertions like assertThat(x).isEqualTo(y) and is recommended when readability matters.

How do I run JUnit 5 tests with Maven or Gradle?

Place tests in src/test/java with a Test suffix on class names, then run mvn test for Maven or gradle test for Gradle. Ensure junit-jupiter-api and junit-jupiter-engine are declared as test dependencies.