java-junit
github/awesome-copilot
JUnit 5 best practices and data-driven testing patterns for Java unit tests.
What is java-junit?
This skill provides guidance on writing effective unit tests with JUnit 5, including standard test structure, parameterized/data-driven tests, assertions, mocking, and test organization. Use it when setting up or improving test suites in Java projects.
- Write standard unit tests following Arrange-Act-Assert pattern with descriptive naming
- Create parameterized tests using @ParameterizedTest with @ValueSource, @MethodSource, @CsvSource, and @CsvFileSource
- Organize and isolate tests using @BeforeEach, @AfterEach, @BeforeAll, @AfterAll lifecycle annotations
- Mock dependencies with Mockito using @Mock and @InjectMocks for test isolation
- Group and categorize tests with @Tag, @Nested, and @TestMethodOrder for better organization
- Use fluent assertions with AssertJ and grouped assertions with assertAll for clarity
How to install java-junit
npx skills add https://github.com/github/awesome-copilot --skill java-junit- Maven or Gradle project with standard src/test/java structure
- JUnit 5 dependencies: junit-jupiter-api, junit-jupiter-engine, junit-jupiter-params
- Optionally: Mockito for mocking, AssertJ for fluent assertions
How to use java-junit
- 1.Add JUnit 5 and parameterized test dependencies to your pom.xml or build.gradle
- 2.Create test classes with Test suffix in src/test/java following package structure
- 3.Write test methods with @Test annotation using Arrange-Act-Assert pattern and descriptive names
- 4.For data-driven tests, use @ParameterizedTest with appropriate source annotation (@ValueSource, @CsvSource, @MethodSource, etc.)
- 5.Use @BeforeEach/@AfterEach for per-test setup/teardown and @BeforeAll/@AfterAll for class-level setup
- 6.Apply @Mock and @InjectMocks for dependency injection and Mockito mocking
- 7.Organize tests with @Tag for categorization and @Nested for grouping related test classes
- 8.Run tests with mvn test or gradle test
Use cases
- Writing parameterized tests that validate a function across multiple input/output combinations from CSV or method sources
- Setting up test fixtures and teardown logic for database or resource-heavy tests
- Organizing large test suites into nested classes by feature or component
- Mocking external service dependencies to test business logic in isolation
- Creating tagged test groups (e.g., 'fast', 'integration') for selective test execution
- Java developers writing unit tests
- QA engineers automating test coverage
- Teams adopting or upgrading to JUnit 5
- Projects using Maven or Gradle build tools
java-junit FAQ
@BeforeEach runs before each test method (can use instance fields), while @BeforeAll runs once before all tests in the class (must be static). Use @BeforeEach for per-test setup like creating fresh objects, and @BeforeAll for expensive one-time setup like database initialization.
Use assertThrows(ExceptionType.class, () -> { code that should throw }). For testing that no exception is thrown, use assertDoesNotThrow.
Use @ValueSource for simple literals, @CsvSource for inline comma-separated values, @MethodSource for complex objects from a factory method, and @CsvFileSource to load data from a CSV file on the classpath.
Both work; AssertJ provides more fluent, readable syntax (assertThat(...).is...) and better error messages. Standard Assertions are sufficient for simple tests. Choose based on team preference and readability needs.
Use @Disabled on the test method or class with a reason string, e.g., @Disabled("Waiting for bug fix"). This prevents the test from running while documenting why.
Full instructions (SKILL.md)
Source of truth, from github/awesome-copilot.
name: java-junit description: 'Get best practices for JUnit 5 unit testing, including data-driven tests'
JUnit 5+ Best Practices
Your goal is to help me write effective unit tests with JUnit 5, covering both standard and data-driven testing approaches.
Project Setup
- Use a standard Maven or Gradle project structure.
- Place test source code in
src/test/java. - Include dependencies for
junit-jupiter-api,junit-jupiter-engine, andjunit-jupiter-paramsfor parameterized tests. - Use build tool commands to run tests:
mvn testorgradle test.
Test Structure
- Test classes should have a
Testsuffix, e.g.,CalculatorTestfor aCalculatorclass. - Use
@Testfor test methods. - Follow the Arrange-Act-Assert (AAA) pattern.
- Name tests using a descriptive convention, like
methodName_should_expectedBehavior_when_scenario. - Use
@BeforeEachand@AfterEachfor per-test setup and teardown. - Use
@BeforeAlland@AfterAllfor per-class setup and teardown (must be static methods). - Use
@DisplayNameto provide a human-readable name for test classes and methods.
Standard Tests
- Keep tests focused on a single behavior.
- Avoid testing multiple conditions in one test method.
- Make tests independent and idempotent (can run in any order).
- Avoid test interdependencies.
Data-Driven (Parameterized) Tests
- Use
@ParameterizedTestto mark a method as a parameterized test. - Use
@ValueSourcefor simple literal values (strings, ints, etc.). - Use
@MethodSourceto refer to a factory method that provides test arguments as aStream,Collection, etc. - Use
@CsvSourcefor inline comma-separated values. - Use
@CsvFileSourceto use a CSV file from the classpath. - Use
@EnumSourceto use enum constants.
Assertions
- Use the static methods from
org.junit.jupiter.api.Assertions(e.g.,assertEquals,assertTrue,assertNotNull). - For more fluent and readable assertions, consider using a library like AssertJ (
assertThat(...).is...). - Use
assertThrowsorassertDoesNotThrowto test for exceptions. - Group related assertions with
assertAllto ensure all assertions are checked before the test fails. - Use descriptive messages in assertions to provide clarity on failure.
Mocking and Isolation
- Use a mocking framework like Mockito to create mock objects for dependencies.
- Use
@Mockand@InjectMocksannotations from Mockito to simplify mock creation and injection. - Use interfaces to facilitate mocking.
Test Organization
- Group tests by feature or component using packages.
- Use
@Tagto categorize tests (e.g.,@Tag("fast"),@Tag("integration")). - Use
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)and@Orderto control test execution order when strictly necessary. - Use
@Disabledto temporarily skip a test method or class, providing a reason. - Use
@Nestedto group tests in a nested inner class for better organization and structure.
Related skills
More from github/awesome-copilot and the wider catalog.

java-mcp-server-generator
Generate production-ready MCP servers in Java with the official SDK, Maven/Gradle, and optional Spring Boot.

java-refactoring-extract-method
Refactor Java methods by extracting complex code blocks into smaller, reusable methods.

java-refactoring-remove-parameter
Prompts an AI agent to remove unused or redundant parameters from Java methods.

java-springboot
Best practices for building high-quality Spring Boot applications with proper architecture and configuration.

javascript-typescript-jest
Best practices for writing JavaScript/TypeScript tests using Jest, including mocking strategies, test structure, and common patterns.

kotlin-mcp-server-generator
Generate production-ready Kotlin MCP servers with Gradle, official SDK, and typed tools.