PluginBench
Skill
Official
Review
Audit score 70

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
Prerequisites
  • 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
Claude Code
Cursor
Windsurf
Cline

How to use java-junit

  1. 1.Add JUnit 5 and parameterized test dependencies to your pom.xml or build.gradle
  2. 2.Create test classes with Test suffix in src/test/java following package structure
  3. 3.Write test methods with @Test annotation using Arrange-Act-Assert pattern and descriptive names
  4. 4.For data-driven tests, use @ParameterizedTest with appropriate source annotation (@ValueSource, @CsvSource, @MethodSource, etc.)
  5. 5.Use @BeforeEach/@AfterEach for per-test setup/teardown and @BeforeAll/@AfterAll for class-level setup
  6. 6.Apply @Mock and @InjectMocks for dependency injection and Mockito mocking
  7. 7.Organize tests with @Tag for categorization and @Nested for grouping related test classes
  8. 8.Run tests with mvn test or gradle test

Use cases

Good for
  • 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
Who it's for
  • 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

What is the difference between @BeforeEach and @BeforeAll?

@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.

How do I test that an exception is thrown?

Use assertThrows(ExceptionType.class, () -> { code that should throw }). For testing that no exception is thrown, use assertDoesNotThrow.

What is the best way to provide test data for parameterized tests?

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.

Should I use AssertJ or standard JUnit assertions?

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.

How do I skip a test temporarily?

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, and junit-jupiter-params for parameterized tests.
  • Use build tool commands to run tests: mvn test or gradle test.

Test Structure

  • Test classes should have a Test suffix, e.g., CalculatorTest for a Calculator class.
  • Use @Test for test methods.
  • Follow the Arrange-Act-Assert (AAA) pattern.
  • Name tests using a descriptive convention, like methodName_should_expectedBehavior_when_scenario.
  • Use @BeforeEach and @AfterEach for per-test setup and teardown.
  • Use @BeforeAll and @AfterAll for per-class setup and teardown (must be static methods).
  • Use @DisplayName to 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 @ParameterizedTest to mark a method as a parameterized test.
  • Use @ValueSource for simple literal values (strings, ints, etc.).
  • Use @MethodSource to refer to a factory method that provides test arguments as a Stream, Collection, etc.
  • Use @CsvSource for inline comma-separated values.
  • Use @CsvFileSource to use a CSV file from the classpath.
  • Use @EnumSource to 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 assertThrows or assertDoesNotThrow to test for exceptions.
  • Group related assertions with assertAll to 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 @Mock and @InjectMocks annotations from Mockito to simplify mock creation and injection.
  • Use interfaces to facilitate mocking.

Test Organization

  • Group tests by feature or component using packages.
  • Use @Tag to categorize tests (e.g., @Tag("fast"), @Tag("integration")).
  • Use @TestMethodOrder(MethodOrderer.OrderAnnotation.class) and @Order to control test execution order when strictly necessary.
  • Use @Disabled to temporarily skip a test method or class, providing a reason.
  • Use @Nested to group tests in a nested inner class for better organization and structure.