Skip to content
OpenSmartRoute
Skillv1.0.0

java-junit

JUnit 5 testing patterns and parameterized-test guidance. Use when writing or reviewing Java unit tests.

by practicalswan(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from practicalswan/agent-skills (java-junit/SKILL.md). Install upstream with npx skills add practicalswan/agent-skills --skill java-junit. Copyright stays with the author.

JUnit 5+ Best Practices

Optimized for current Java LTS releases, JUnit 5.x, Mockito 5.x, and modern Maven or Gradle builds.

Your goal is to help me write effective unit tests with JUnit 5, covering both standard and data-driven testing approaches.

  • Leverage native parallel subagent dispatch and 200k+ context windows where available.

Anti-Patterns

  • Combining multiple behaviors in one test: A single failure should map cleanly to one broken contract.
  • Using sleeps for asynchronous behavior: Time-based tests stay flaky even when the implementation is correct.
  • Testing implementation details instead of behavior: Refactors become noisy because the tests are coupled to internals.

Verification Protocol

Before claiming "skill applied successfully":

  1. Pass/fail: The Java Junit implementation names the target runtime, framework version, and affected files.
  2. Pass/fail: Build, lint, test, or equivalent local validation is run for the changed surface.
  3. Pass/fail: Edge cases for errors, dependency drift, and environment differences are addressed or explicitly out of scope.
  4. Pressure-test scenario: Apply the workflow to a change that passes happy-path tests but fails one boundary condition.
  5. Success metric: Zero untested success claims; every implementation claim maps to a command or artifact.

Before and After Example

// Before
@Test
void shouldWork() {
    assertTrue(service.create(user));
}

// After
@Test
void create_WhenEmailAlreadyExists_ThrowsConflictException() {
    // Arrange
    repository.save(existingUser);

    // Act + Assert
    assertThrows(ConflictException.class, () -> service.create(duplicateUser));
}

Shifts from a vague happy-path assertion to a precise behavioral test that captures the contract.

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.

Common Pitfalls

  • Combining multiple behaviors in one test: Failures become ambiguous and the test no longer documents a single contract.
  • Using sleeps instead of deterministic setup: Time-based tests stay flaky even when the production code is correct.
  • Asserting only that no exception occurred: A test without a meaningful assertion cannot prove the behavior is right.

Cross-Client Portability

This skill is written to stay usable across GitHub Copilot, Claude Code, and Codex.

  • GitHub Copilot: keep the folder in a Copilot-visible skill path or wrap the workflow in project instructions when folder discovery is unavailable.
  • Claude Code: keep the folder in a local skills directory or a compatible plugin source.
  • Codex: install or sync the folder into $CODEX_HOME/skills/java-junit and restart Codex after major changes.

MCP Availability And Fallback

Preferred MCP Server: None required

  • Fallback prompt: "Use the JUnit 5+ Best Practices skill without MCP. Rely on the local SKILL.md, bundled references or scripts, and manual verification. Show the exact commands, evidence, and final checks you used before concluding."
  • If the current host does not expose a matching server, use the bundled references, scripts, native toolchain, and manual workflow already described in this skill.
  • Treat direct local verification, rendered output, logs, tests, or screenshots as the fallback evidence path before completion.

Related Skills

  • java-docs: Use it when the workflow also needs Java API and JavaDoc documentation guidance.
  • test-driven-development: Use it when the workflow also needs test-first implementation and regression safety.
  • code-quality: Use it when the workflow also needs two-stage review (spec compliance first, then code quality), maintainability, and refactoring guidance.
  • systematic-debugging: Use it when the workflow also needs root-cause debugging before proposing fixes.

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/practicalswan-agent-skills-java-junit/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

practicalswan-agent-skills-java-junit.ocm.jsonjson
{
  "ocm": "1",
  "id": "practicalswan-agent-skills-java-junit",
  "kind": "skill",
  "name": "java-junit",
  "description": "JUnit 5 testing patterns and parameterized-test guidance. Use when writing or reviewing Java unit tests.",
  "publisher": "practicalswan",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "coding"
    ],
    "tags": [
      "skill-md",
      "java",
      "testing",
      "development",
      "quality",
      "automation",
      "skills-sh"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "JUnit 5 testing patterns and parameterized-test guidance. Use when writing or reviewing Java unit tests."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "skills.sh",
      "repository": "https://github.com/practicalswan/agent-skills",
      "path": "java-junit/SKILL.md",
      "ref": "HEAD",
      "url": "https://github.com/practicalswan/agent-skills/blob/HEAD/java-junit/SKILL.md",
      "key": "practicalswan/agent-skills/java-junit/SKILL.md"
    }
  },
  "instructions": "# JUnit 5+ Best Practices\n\n> Optimized for current Java LTS releases, JUnit 5.x, Mockito 5.x, and modern Maven or Gradle builds.\n\nYour goal is to help me write effective unit tests with JUnit 5, covering both standard and data-driven testing approaches.\n\n- Leverage native parallel subagent dispatch and 200k+ context windows where available.\n\n\n## Anti-Patterns\n\n- Combining multiple behaviors in one test: A single failure should map cleanly to one broken contract.\n- Using sleeps for asynchronous behavior: Time-based tests stay flaky even when the implementation is correct.\n- Testing implementati",
  "cost": {
    "context_tokens": 1663
  }
}

Fetch it by URL: GET /api/v1/registry/practicalswan-agent-skills-java-junit/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.