skillfed

junit-5-skill

This skill equips you to write robust JUnit 5 tests in Java, from basic assertions and exception handling to parameterized test cases and Mockito-driven mocking. It covers lifecycle management, nested test organization, and anti-patterns to avoid, with quick reference commands for Maven and Gradle execution.

junit-5-skill generates production-grade JUnit 5 tests with assertions, parameterized tests, and Mockito mocking in Java.

AI-generated summary based on this skill's SKILL.md

339 68 MIT updated by LambdaTest

Install

LambdaTest/agent-skills/junit-5-skill · repository language: Python

git clone https://github.com/LambdaTest/agent-skills
cp -r agent-skills/junit-5-skill ~/.claude/skills/junit-5-skill
npx skillfed install LambdaTest/agent-skills/junit-5-skill

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How to write JUnit 5 tests in Java?

junit-5-skill teaches you to write JUnit 5 tests by marking test methods with @Test, using assertions like assertEquals and assertThrows, and organizing tests with @BeforeEach and @AfterEach lifecycle hooks. Start with a simple test class, add your test methods, and run via Maven or Gradle. The skill covers both basic unit tests and advanced patterns like parameterized tests and nested test classes for better organization.

What are JUnit 5 annotations and best practices?

junit-5-skill covers key JUnit 5 annotations including @Test, @BeforeEach, @AfterEach, @DisplayName, @Nested, and @ParameterizedTest. Best practices include using descriptive test names, organizing related tests in nested classes, leveraging @DisplayName for readable output, keeping tests focused and independent, and using parameterized tests to reduce duplication. The skill also teaches when to mock versus when to use real objects.

How do I mock dependencies in JUnit with Mockito?

junit-5-skill shows how to mock dependencies using Mockito by creating mock objects with Mockito.mock(), stubbing behavior with when().thenReturn(), and verifying method calls with verify(). You can inject mocks into your test class and configure them to return specific values or throw exceptions. The skill demonstrates common patterns like mocking external services, verifying interactions, and using argument matchers for flexible assertions.

What are parameterized tests and how do I create them?

junit-5-skill explains parameterized tests as a way to run the same test logic with multiple input sets, reducing code duplication. Use @ParameterizedTest with sources like @ValueSource, @CsvSource, or @MethodSource to provide test data. For example, @CsvSource lets you inline multiple test cases as CSV rows. This approach makes it easy to test edge cases and boundary conditions without writing separate test methods for each scenario.

How do I organize tests with nested classes and lifecycle?

junit-5-skill teaches test organization using @Nested to group related tests into inner classes, improving readability and maintainability. Combine this with lifecycle hooks: @BeforeEach runs before each test, @BeforeAll runs once before all tests in a class, and @AfterEach/@AfterAll clean up afterward. Nested classes inherit parent lifecycle hooks, allowing you to set up shared context and keep tests logically grouped by feature or scenario.

What assertions and exception testing does JUnit 5 provide?

junit-5-skill covers JUnit 5 assertions including assertEquals, assertTrue, assertFalse, assertNull, assertThrows, and assertAll. Use assertThrows to verify that code throws an expected exception with a specific message. Use assertAll to group multiple assertions so all run even if one fails, giving you complete feedback. The skill also shows how to use custom assertion messages and lambda expressions for more expressive, maintainable tests.

SKILL.md

rendered from the published skill — quoted content, verbatim

JUnit 5 Testing Skill

You are a senior Java developer specializing in JUnit 5 testing.

Step 1 — Test Type

├─ "unit test", "assert" → Standard unit test
├─ "parameterized", "multiple inputs" → @ParameterizedTest
├─ "mock", "Mockito" → Unit test with Mockito
├─ "integration test", "Spring" → Read reference/spring-integration.md
└─ Default → Standard unit test

Core Patterns

Basic Test

```java import org.junit.jupiter.api.; import static org.junit.jupiter.api.Assertions.;

class CalculatorTest { private Calculator calculator;

@BeforeEach
void setUp() {
    calculator = new Calculator();
}

@Test
@DisplayName("Addition of two positive

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 3 files
junit-5-skill/SKILL.md
junit-5-skill/reference/advanced-patterns.md
junit-5-skill/reference/playbook.md

Related skills

Tags

test-automation assertion-library mock-framework test-organization java-testing tdd-patterns ci-cd-ready parameterization