agentleFS
vekzz-devvekzz-dev/opencode-skills/skills/java-junit/SKILL.md

java-junit

Skill4 starsChanged 40 days agoMITView on GitHub

Best practices for JUnit 5 unit testing, including data-driven tests and modern patterns. Trigger: When writing JUnit tests, creating unit tests, or need testing best practices.

4 other projects use a version of this, 3 of them edited.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

29 rules. 7 are common, 0 are distinctive, and 22 are very specific to this project.

  • 7

    Common

    Most of the 4 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 22

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Common:

    Use a standard Maven or Gradle project structure

    line 23
  2. Very specific:

    Place test source code in src/test/java

    line 24
  3. Very specific:

    Use build tool commands to run tests: mvn test or gradle test

    line 26
  4. Very specific:

    Test classes should have a Test suffix, e.g., CalculatorTest for a Calculator class

    line 30
  5. Very specific:

    Use @Test for test methods

    line 31
  6. Common:

    Follow the Arrange-Act-Assert (AAA) pattern

    line 32
  7. Very specific:

    Use @BeforeEach and @AfterEach for per-test setup and teardown

    line 34
  8. Very specific:

    Use @BeforeAll and @AfterAll for per-class setup and teardown (must be static methods)

    line 35
  9. Very specific:

    Use @DisplayName to provide a human-readable name for test classes and methods

    line 36
  10. Common:

    Keep tests focused on a single behavior

    line 73
  11. Common:

    Avoid testing multiple conditions in one test method

    line 74
  12. Very specific:

    Use @ParameterizedTest to mark a method as a parameterized test

    line 80
  13. Very specific:

    Use @ValueSource for simple literal values (strings, ints, etc.)

    line 81
  14. Very specific:

    Use @MethodSource to refer to a factory method that provides test arguments as a Stream, Collection, etc.

    line 82
  15. Very specific:

    Use @CsvSource for inline comma-separated values

    line 83
  16. Very specific:

    Use @CsvFileSource to use a CSV file from the classpath

    line 84
  17. Very specific:

    Use @EnumSource to use enum constants

    line 85
  18. Very specific:

    Use the static methods from org.junit.jupiter.api.Assertions (e.g., assertEquals, assertTrue, assertNotNull)

    line 124
  19. Very specific:

    Use assertThrows or assertDoesNotThrow to test for exceptions

    line 126
  20. Common:

    Use descriptive messages in assertions to provide clarity on failure

    line 128
  21. Common:

    Use a mocking framework like Mockito to create mock objects for dependencies

    line 160
  22. Very specific:

    Use @Mock and @InjectMocks annotations from Mockito to simplify mock creation and injection

    line 161
  23. Common:

    Use interfaces to facilitate mocking

    line 162
  24. Very specific:

    Use @Tag to categorize tests (e.g., @Tag("fast"), @Tag("integration"))

    line 209
  25. Very specific:

    Use @TestMethodOrder(MethodOrderer.OrderAnnotation.class) and @Order to control test execution order when strictly necessary

    line 210
  26. Very specific:

    Use @Disabled to temporarily skip a test method or class, providing a reason

    line 211
  27. Very specific:

    Use @Nested to group tests in a nested inner class for better organization and structure

    line 212
  28. Very specific:

    Resolve the libraryId: - Use context7_resolve-library-id:

    line 275
  29. Very specific:

    Query the docs: - Use context7_query-docs:

    line 279

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

When to Use

  • Writing JUnit 5 unit tests
  • Creating parameterized tests
  • Setting up test infrastructure
  • Need testing best practices for Java

Instructions

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
class CalculatorTest {

    private Calculator calculator;

    @BeforeEach
    void setUp() {
        calculator = new Calculator();
    }

    @Test
    @DisplayName("Should add two positive numbers")
    void add_shouldReturnSum_whenBothPositive() {
        // Arrange
        int a = 3, b = 5;

        // Act
        int result = calculator.add(a, b);

        // Assert
        assertEquals(8, result);
    }

    @Test
    @DisplayName("Should throw exception when dividing by zero")
    void divide_shouldThrowException_whenDenominatorIsZero() {
        assertThrows(ArithmeticException.class,
            () -> calculator.divide(10, 0),
            "Division by zero should throw");
    }
}

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
@ParameterizedTest
@ValueSource(ints = {1, 2, 3, 5, 8, 13})
@DisplayName("Should identify Fibonacci numbers")
void isFibonacci_shouldReturnTrue_forFibonacciNumbers(int number) {
    assertTrue(calculator.isFibonacci(number));
}

@ParameterizedTest
@CsvSource({
    "4, 2, 2",
    "10, 3, 1",
    "7, 5, 2"
})
@DisplayName("Should compute modulo for various inputs")
void modulo_shouldReturnRemainder(int dividend, int divisor, int expected) {
    assertEquals(expected, calculator.modulo(dividend, divisor));
}

@ParameterizedTest
@MethodSource("provideOrderStatuses")
@DisplayName("Should return correct display name for each status")
void getDisplayName_shouldReturnCorrectLabel(OrderStatus status, String expected) {
    assertEquals(expected, status.getDisplayName());
}

static Stream<Arguments> provideOrderStatuses() {
    return Stream.of(
        Arguments.of(OrderStatus.PENDING, "Pending"),
        Arguments.of(OrderStatus.SHIPPED, "Shipped"),
        Arguments.of(OrderStatus.DELIVERED, "Delivered")
    );
}

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
@Test
@DisplayName("Should create order with all required fields")
void createOrder_shouldSetAllFields() {
    Order order = orderService.create("customer-1", List.of("item-a"), 2990);

    assertAll("order fields",
        () -> assertEquals("customer-1", order.getCustomerId(), "customer id"),
        () -> assertEquals(2, order.getItemCount(), "item count"),
        () -> assertNotNull(order.getCreatedAt(), "creation timestamp"),
        () -> assertEquals(OrderStatus.PENDING, order.getStatus(), "initial status")
    );
}

// AssertJ example
@Test
@DisplayName("Should return orders sorted by date")
void findRecentOrders_shouldReturnSortedResults() {
    List<Order> orders = orderService.findRecent(7);

    assertThat(orders)
        .isNotEmpty()
        .hasSizeLessThanOrEqualTo(50)
        .extracting(Order::getStatus)
        .contains(OrderStatus.SHIPPED);
}

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
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {

    @Mock
    private PaymentGateway paymentGateway;

    @Mock
    private InventoryClient inventoryClient;

    @InjectMocks
    private OrderService orderService;

    @Test
    @DisplayName("Should process payment when placing order")
    void placeOrder_shouldProcessPayment() {
        // Arrange
        when(inventoryClient.isAvailable("item-1")).thenReturn(true);
        when(paymentGateway.charge(any(Payment.class)))
            .thenReturn(new PaymentResult("tx-123", PaymentStatus.SUCCESS));

        // Act
        Order order = orderService.placeOrder("customer-1", "item-1");

        // Assert
        assertThat(order.getTransactionId()).isEqualTo("tx-123");
        verify(paymentGateway).charge(any(Payment.class));
    }

    @Test
    @DisplayName("Should throw when inventory is insufficient")
    void placeOrder_shouldThrow_whenItemUnavailable() {
        when(inventoryClient.isAvailable("item-1")).thenReturn(false);

        assertThrows(InsufficientInventoryException.class,
            () -> orderService.placeOrder("customer-1", "item-1"));

        verify(paymentGateway, never()).charge(any());
    }
}

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
@DisplayName("OrderService")
class OrderServiceTest {

    @Nested
    @DisplayName("placeOrder")
    class PlaceOrder {

        @Test
        @DisplayName("Should create order successfully")
        void shouldCreateOrder() { }

        @Test
        @DisplayName("Should reject duplicate orders")
        void shouldRejectDuplicate() { }
    }

    @Nested
    @DisplayName("cancelOrder")
    class CancelOrder {

        @Test
        @DisplayName("Should cancel pending order")
        void shouldCancelPending() { }

        @Test
        @DisplayName("Should throw when order is already shipped")
        void shouldThrowWhenShipped() { }
    }
}

Commands

# Run all tests
mvn test

# Run specific test class
mvn test -Dtest=CalculatorTest

# Run tests with specific tag
mvn test -Dgroups=fast

# Run tests with Gradle
gradle test

# Run specific test with Gradle
gradle test --tests CalculatorTest

Resources

Context7 Integration

For up-to-date information on JUnit 5 and AssertJ:

  1. Resolve the libraryId - Use context7_resolve-library-id:
    • libraryName: "junit 5", "assertj", or "mockito"
    • query: what you need (e.g., "parameterized tests", "assertions")
  2. Query the docs - Use context7_query-docs:
    • libraryId: "/junit/junit5" or "/assertj/assertj"
    • query: your specific question

Before writing tests, consult Context7 to get updated examples.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.