agentleFS
Marisha-SahayMarisha-Sahay/awesome-java-agent-skills/skills/testing/junit5-mockito-testcontainers/SKILL.md

junit5-mockito-testcontainers

Skill22 starsChanged 3 months agoMITView on GitHub

Guidelines for writing robust, modern Java tests using JUnit 5, Mockito, and Testcontainers.

The file

Files
SKILL.md

Testing Mastery Skill

This skill focuses on best practices for writing high-quality tests in modern Java applications.

Testing Pattern

Enforce the AAA (Arrange, Act, Assert) or BDD (Given, When, Then) pattern in all tests to ensure tests are highly readable and structured consistently.

Example Context:

@Test
void shouldCalculateTotalCorrectly() {
    // Arrange
    Order order = new Order(100.0, 0.1); // price, discount
    
    // Act
    double total = orderService.calculateTotal(order);
    
    // Assert
    assertThat(total).isEqualTo(90.0);
}

Mocking

Instruct the agent to avoid over-mocking. Mock external boundaries and slow dependencies, but use real domain objects. Avoid mocking simple value types or internal application logic when possible. Use Mockito 5+ for all mocking needs.

Example Context:

@ExtendWith(MockitoExtension.class)
class OrderServiceTest {

    @Mock
    private PaymentClient paymentClient; // External boundary

    @InjectMocks
    private OrderService orderService;

    @Test
    void shouldProcessPayment() {
        // Arrange
        OrderRequest request = new OrderRequest("C123", new BigDecimal("100.0"));
        given(paymentClient.charge(any())).willReturn(true);

        // Act & Assert
        assertThat(orderService.process(request)).isTrue();
        verify(paymentClient).charge(any());
    }
}

Integration Tests

Mandate the use of Testcontainers for database integration testing instead of relying on in-memory databases like H2. This ensures environment parity and prevents bugs that only occur in the production database system.

Example Context:

@Testcontainers
@SpringBootTest
class OrderRepositoryTest {

    @Container
    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15-alpine");

    @DynamicPropertySource
    static void registerPgProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgres::getJdbcUrl);
        registry.add("spring.datasource.username", postgres::getUsername);
        registry.add("spring.datasource.password", postgres::getPassword);
    }
    
    // tests here...
}

Frameworks

Specify JUnit 5 (Jupiter) for the testing framework and Mockito 5+ for mocking.

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.