junit5-mockito-testcontainers
Skill24 starsChanged 3 months agoMITView on GitHub
Use when writing or fixing Java tests with JUnit 5, Mockito or Testcontainers, when a test mocks domain objects or value types, or when a Spring Boot integration test runs against H2 or another in-memory database instead of the production database.
In this repository4 other files it gives its agents.
The file
We split this skill into the rules it gives an agent
6 rules. About 0 are common, 2 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 31
When writing the test before the code, a compile error is not the failing test: a test that doesn't compile never ran, so it shows nothing …
- Very specific: line 34
Add only the signatures it needs (constructor, method), with a body that returns a deliberately wrong value.
- Very specific: line 35
Run the test and confirm Failures: 1 with the assertion message you expect, such as Expecting value to be false but was true.
- Very specific: line 38
If a stub's default value already passes the test (a stub returning false passes a test that expects false), the test can't tell the stub …
- Very specific: line 76
When only one layer is under test, prefer a test slice such as @WebMvcTest over @SpringBootTest, and replace its collaborators with …
- Distinctive: line 129
Use JUnit Jupiter (JUnit 5 with Spring Boot 3, JUnit 6 with Spring Boot 4), Mockito 5+ and AssertJ.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
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 appliesTheDiscountToTheOrderTotal() {
// Arrange
Order order = new Order(new BigDecimal("100.00"), new BigDecimal("0.10")); // price, discount rate
// Act
BigDecimal total = orderService.calculateTotal(order);
// Assert
assertThat(total).isEqualByComparingTo("90.00");
}Model money as BigDecimal, never double, and compare it with isEqualByComparingTo. isEqualTo also compares scale, so 90.0 and 90.00 are not equal under it.
Test-First in Java
When writing the test before the code, a compile error is not the failing test: a test that doesn't compile never ran, so it shows nothing about whether its assertions can fail.
- Write the test.
- Add only the signatures it needs (constructor, method), with a body that returns a deliberately wrong value. Don't throw from the stub: Surefire reports an exception as
Errors: 1, not as a failed assertion. - Run the test and confirm
Failures: 1with the assertion message you expect, such asExpecting value to be false but was true. - Implement, then run it again and see it pass.
If a stub's default value already passes the test (a stub returning false passes a test that expects false), the test can't tell the stub from the real rule: add the opposite case before implementing.
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 chargesTheCustomerForTheOrderAmount() {
// Arrange
OrderRequest request = new OrderRequest("C123", new BigDecimal("100.00"));
given(paymentClient.charge(any(Charge.class))).willReturn(true);
// Act
boolean processed = orderService.process(request);
// Assert
assertThat(processed).isTrue();
ArgumentCaptor<Charge> charge = ArgumentCaptor.forClass(Charge.class);
then(paymentClient).should().charge(charge.capture());
assertThat(charge.getValue().customerId()).isEqualTo("C123");
assertThat(charge.getValue().amount()).isEqualByComparingTo("100.00");
}
}verify(paymentClient).charge(any()) passes whatever amount is charged. When the arguments are what the test is about, capture them and assert their values. On paths that must not reach the boundary, such as a rejected order, assert then(paymentClient).shouldHaveNoInteractions().
Mocking Spring Beans
When only one layer is under test, prefer a test slice such as @WebMvcTest over @SpringBootTest, and replace its collaborators with @MockitoBean (Spring Framework 6.2+, Spring Boot 3.4+). @MockBean is deprecated since Spring Boot 3.4 and removed in Spring Boot 4.
Example Context:
@WebMvcTest(OrderController.class)
class OrderControllerTest {
@Autowired
private MockMvc mockMvc;
@MockitoBean
private OrderService orderService;
@Test
void returns404WhenTheOrderDoesNotExist() throws Exception {
given(orderService.find("O-404")).willThrow(new OrderNotFoundException("O-404"));
mockMvc.perform(get("/api/orders/O-404"))
.andExpect(status().isNotFound());
}
}Asynchronous Code
Awaitility comes with spring-boot-starter-test. To assert on work done by @Async methods, event listeners or message consumers, poll with it instead of Thread.sleep or a hand-written loop:
await().atMost(Duration.ofSeconds(5))
.untilAsserted(() -> assertThat(auditLog.entriesFor("O-200")).hasSize(1));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
@ServiceConnection
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine");
// tests here...
}@ServiceConnection (Spring Boot 3.1+, from the spring-boot-testcontainers test dependency) points the datasource at the container. It replaces a @DynamicPropertySource method that copies the JDBC URL, username and password; keep @DynamicPropertySource only for properties no service connection covers.
With Spring Boot 4 (Testcontainers 2), the module is org.testcontainers:testcontainers-postgresql and the class is org.testcontainers.postgresql.PostgreSQLContainer, which is no longer generic: declare static PostgreSQLContainer postgres.
Frameworks
Use JUnit Jupiter (JUnit 5 with Spring Boot 3, JUnit 6 with Spring Boot 4), Mockito 5+ and AssertJ.
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
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.

