Skillshub java-junit
Get best practices for JUnit 5 unit testing, including data-driven tests
install
source · Clone the upstream repo
git clone https://github.com/ComeOnOliver/skillshub
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ComeOnOliver/skillshub "$T" && mkdir -p ~/.claude/skills && cp -r "$T/skills/github/awesome-copilot/java-junit" ~/.claude/skills/comeonoliver-skillshub-java-junit && rm -rf "$T"
manifest:
skills/github/awesome-copilot/java-junit/SKILL.mdsource content
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
, andjunit-jupiter-engine
for parameterized tests.junit-jupiter-params - Use build tool commands to run tests:
ormvn test
.gradle test
Test Structure
- Test classes should have a
suffix, e.g.,Test
for aCalculatorTest
class.Calculator - Use
for test methods.@Test - Follow the Arrange-Act-Assert (AAA) pattern.
- Name tests using a descriptive convention, like
.methodName_should_expectedBehavior_when_scenario - Use
and@BeforeEach
for per-test setup and teardown.@AfterEach - Use
and@BeforeAll
for per-class setup and teardown (must be static methods).@AfterAll - Use
to provide a human-readable name for test classes and methods.@DisplayName
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
to mark a method as a parameterized test.@ParameterizedTest - Use
for simple literal values (strings, ints, etc.).@ValueSource - Use
to refer to a factory method that provides test arguments as a@MethodSource
,Stream
, etc.Collection - Use
for inline comma-separated values.@CsvSource - Use
to use a CSV file from the classpath.@CsvFileSource - Use
to use enum constants.@EnumSource
Assertions
- Use the static methods from
(e.g.,org.junit.jupiter.api.Assertions
,assertEquals
,assertTrue
).assertNotNull - For more fluent and readable assertions, consider using a library like AssertJ (
).assertThat(...).is... - Use
orassertThrows
to test for exceptions.assertDoesNotThrow - Group related assertions with
to ensure all assertions are checked before the test fails.assertAll - 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
and@Mock
annotations from Mockito to simplify mock creation and injection.@InjectMocks - Use interfaces to facilitate mocking.
Test Organization
- Group tests by feature or component using packages.
- Use
to categorize tests (e.g.,@Tag
,@Tag("fast")
).@Tag("integration") - Use
and@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
to control test execution order when strictly necessary.@Order - Use
to temporarily skip a test method or class, providing a reason.@Disabled - Use
to group tests in a nested inner class for better organization and structure.@Nested