Index of every page on this site: llms.txt
All skills
github avatar

/java-junit

@f10fdb8 official
by githubgithub/awesome-copilot42k stars
5,426
Run it yourself

Write, review and fix Java unit tests with JUnit 5 and JUnit 6 (Jupiter), AssertJ and Mockito: assertions, parameterized and nested tests, mocking rules, deterministic time and randomness, Surefire vs Failsafe, JaCoCo coverage, optional PIT mutation testing, and guardrails for agents (never skip or delete failing tests). Use when adding, refactoring or debugging tests in Maven or Gradle projects. Framework-neutral.

Use this Skill: https://skilld.dev/gh/github/awesome-copilot/java-junit

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesbuild-setup.md

≈1.2k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Build setup for JUnit 5/6

Versions below were used in verification runs (JDK 21 and 25, Maven 3.10.0, Gradle 9.8.1). Re-check current releases on Maven Central before pinning, and keep the project's existing versions when present.

Maven

<properties>
  <junit.version>6.0.3</junit.version>      <!-- 5.14.4 works with the same test code; Java baseline 17 for 6.x -->
</properties>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.junit</groupId>
      <artifactId>junit-bom</artifactId>
      <version>${junit.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>
<build>
  <plugins>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.6.0</version>
    </plugin>
    <plugin>
      <artifactId>maven-failsafe-plugin</artifactId>
      <version>3.6.0</version>
      <executions>
        <execution>
          <goals><goal>integration-test</goal><goal>verify</goal></goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Switching versions for a trial run: mvn verify -Djunit.version=5.14.4 (works because the version is a property). If a parent (e.g. Spring Boot) manages JUnit, override that property or BOM deliberately and tell the user.

Commands: mvn test (unit, Surefire), mvn verify (unit + *IT via Failsafe), mvn -Dtest=FooTest#bar test, mvn -Dit.test=FooIT verify.

JaCoCo (0.8.15)

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.15</version>
  <executions>
    <execution><goals><goal>prepare-agent</goal></goals></execution>
    <execution>
      <id>report</id><phase>verify</phase><goals><goal>report</goal></goals>
    </execution>
    <!-- optional gate; only if the project already enforces one -->
    <execution>
      <id>check</id><phase>verify</phase><goals><goal>check</goal></goals>
      <configuration>
        <rules><rule><element>BUNDLE</element>
          <limits><limit><counter>LINE</counter><value>COVEREDRATIO</value><minimum>0.80</minimum></limit></limits>
        </rule></rules>
      </configuration>
    </execution>
  </executions>
</plugin>

If Surefire/Failsafe set <argLine>, it must start with @{argLine}, otherwise the JaCoCo agent is dropped. Conversely, a literal @{argLine} without a prepare-agent execution fails the forked JVM (Error: could not open '{argLine}', verified). Report: target/site/jacoco/index.html.

PIT mutation testing (verified: pitest-maven 1.30.1 + pitest-junit5-plugin 1.2.3 with JUnit 6.0.3 on JDK 21)

<plugin>
  <groupId>org.pitest</groupId>
  <artifactId>pitest-maven</artifactId>
  <version>1.30.1</version>
  <dependencies>
    <dependency>
      <groupId>org.pitest</groupId>
      <artifactId>pitest-junit5-plugin</artifactId>
      <version>1.2.3</version>
    </dependency>
  </dependencies>
  <configuration>
    <targetClasses><param>com.example.*</param></targetClasses>
  </configuration>
</plugin>

Run: mvn test-compile org.pitest:pitest-maven:mutationCoverage (report in target/pit-reports). PIT reads Surefire's argLine too; if it contains an unresolved @{argLine}, pass -DargLine=-Dpit=1 for the PIT run (verified workaround). Gradle equivalent: the info.solidsoft.pitest plugin (not verified here).

Gradle (Kotlin DSL)

plugins { java; jacoco }
repositories { mavenCentral() }

dependencies {
    testImplementation(platform("org.junit:junit-bom:6.0.3"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")   // required with Gradle 9
}

tasks.test {
    useJUnitPlatform()
    finalizedBy(tasks.jacocoTestReport)
}

Integration tests as a JVM Test Suite (verified; src/integrationTest/java):

testing {
    suites {
        register<JvmTestSuite>("integrationTest") {
            useJUnitJupiter("6.0.3") // a custom suite does not inherit the unit tests' BOM; keep the versions equal
            dependencies { implementation(project()) }
            targets.all { testTask.configure { shouldRunAfter(tasks.test) } }
        }
    }
}
tasks.check { dependsOn(testing.suites.named("integrationTest")) }

Commands: ./gradlew test, ./gradlew test --tests 'demo.OrdersTest' (also runs its nested classes), ./gradlew check, report at build/reports/tests/test/index.html and build/reports/jacoco. Trial run on JUnit 5: ./gradlew test -PjunitVersion=5.14.4 when the version is read from a Gradle property.

Groovy DSL differs only in syntax (testImplementation platform('org.junit:junit-bom:6.0.3'), jvmArgs '-javaagent:...').

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

1 warning13d5 checks · Risk SAFE
  • Gen Agent Trust Hub13d

    The skill provides comprehensive guidance for Java testing using JUnit, AssertJ, and Mockito. It includes standard build configuration snippets and operational commands for Maven and Gradle. No security issues were detected.

  • Socket1mo

    No alerts

  • Snyk1mo

    Risk: LOW · No issues

  • Runlayer9mo

    1/1 file flagged

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at f10fdb8. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 4 days ago.

Activeupdated last month
  • Testing
  • junit
  • junit5
  • java
  • unit-testing
  • parameterized-tests
  • mockito
  • maven
  • gradle

README badge

README badge for github/awesome-copilot/java-junit

Guides JUnit 5 unit testing in Java with emphasis on data-driven tests using parameterized annotations like @ParameterizedTest, @ValueSource, and @MethodSource. Covers test structure, the Arrange-Act-Assert pattern, mocking with Mockito, and organizing tests with @Tag and @Nested for Maven or Gradle projects.

Generated from the current SKILL.md.

Does this skill cover JUnit 5 parameterized tests?
Yes. The skill includes guidance on @ParameterizedTest with @ValueSource, @MethodSource, @CsvSource, @CsvFileSource, and @EnumSource.
What assertion libraries does this skill recommend?
The skill recommends org.junit.jupiter.api.Assertions for standard assertions, and suggests AssertJ as an optional library for more fluent assertion syntax.
Does this skill include mocking guidance?
Yes. The skill covers mocking with Mockito, including @Mock and @InjectMocks annotations for dependency injection and isolation.
What build tools does this skill support?
The skill supports Maven and Gradle, using mvn test and gradle test commands respectively.

Generated from the current SKILL.md. These answers refresh after source changes.