$npx -y skills add rrezartprebreza/spring-boot-skills --skill testing-pyramidUse when writing tests of any kind — unit, slice, or integration. Covers test structure, naming conventions, Mockito patterns, @WebMvcTest, @DataJpaTest, and Testcontainers setup.
| 1 | # Testing Pyramid |
| 2 | |
| 3 | ## Structure |
| 4 | |
| 5 | ``` |
| 6 | Unit Tests — fast, no Spring context, mock dependencies (70%) |
| 7 | Slice Tests — partial Spring context (@WebMvcTest, @DataJpaTest) (20%) |
| 8 | Integration Tests — full context + real DB via Testcontainers (10%) |
| 9 | ``` |
| 10 | |
| 11 | ## Unit Tests — Services |
| 12 | |
| 13 | ```java |
| 14 | @ExtendWith(MockitoExtension.class) |
| 15 | class OrderServiceTest { |
| 16 | |
| 17 | @Mock private OrderRepository orderRepository; |
| 18 | @Mock private InventoryService inventoryService; |
| 19 | |
| 20 | @InjectMocks private OrderService orderService; |
| 21 | |
| 22 | @Test |
| 23 | void createOrder_whenItemsAvailable_shouldSaveAndReturnOrder() { |
| 24 | // Given |
| 25 | var request = new CreateOrderRequest("user@example.com", List.of(new OrderItemRequest(UUID.randomUUID(), 2))); |
| 26 | var savedOrder = Order.create("user@example.com"); |
| 27 | when(orderRepository.save(any(Order.class))).thenReturn(savedOrder); |
| 28 | doNothing().when(inventoryService).reserve(any()); |
| 29 | |
| 30 | // When |
| 31 | Order result = orderService.createOrder(request); |
| 32 | |
| 33 | // Then |
| 34 | assertThat(result).isNotNull(); |
| 35 | assertThat(result.getCustomerEmail()).isEqualTo("user@example.com"); |
| 36 | verify(inventoryService).reserve(request.items()); |
| 37 | verify(orderRepository).save(any(Order.class)); |
| 38 | } |
| 39 | |
| 40 | @Test |
| 41 | void createOrder_whenInventoryUnavailable_shouldThrowException() { |
| 42 | // Given |
| 43 | var request = new CreateOrderRequest("user@example.com", List.of()); |
| 44 | doThrow(new InsufficientInventoryException("Out of stock")) |
| 45 | .when(inventoryService).reserve(any()); |
| 46 | |
| 47 | // When / Then |
| 48 | assertThatThrownBy(() -> orderService.createOrder(request)) |
| 49 | .isInstanceOf(InsufficientInventoryException.class) |
| 50 | .hasMessage("Out of stock"); |
| 51 | } |
| 52 | } |
| 53 | ``` |
| 54 | |
| 55 | ## Slice Tests — Controllers (@WebMvcTest) |
| 56 | |
| 57 | ```java |
| 58 | @WebMvcTest(OrderController.class) |
| 59 | class OrderControllerTest { |
| 60 | |
| 61 | @Autowired MockMvc mockMvc; |
| 62 | @Autowired ObjectMapper objectMapper; |
| 63 | @MockitoBean OrderService orderService; // Spring Boot 3.4+: @MockBean is deprecated |
| 64 | |
| 65 | @Test |
| 66 | @WithMockUser(roles = "USER") |
| 67 | void createOrder_withValidRequest_shouldReturn201() throws Exception { |
| 68 | // Given |
| 69 | var request = new CreateOrderRequest("user@example.com", List.of()); |
| 70 | var order = Order.create("user@example.com"); |
| 71 | when(orderService.createOrder(any())).thenReturn(order); |
| 72 | |
| 73 | // When / Then |
| 74 | mockMvc.perform(post("/api/v1/orders") |
| 75 | .contentType(MediaType.APPLICATION_JSON) |
| 76 | .content(objectMapper.writeValueAsString(request))) |
| 77 | .andExpect(status().isCreated()) |
| 78 | .andExpect(jsonPath("$.success").value(true)) |
| 79 | .andExpect(jsonPath("$.data.customerEmail").value("user@example.com")); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | @WithMockUser |
| 84 | void createOrder_withInvalidRequest_shouldReturn400() throws Exception { |
| 85 | mockMvc.perform(post("/api/v1/orders") |
| 86 | .contentType(MediaType.APPLICATION_JSON) |
| 87 | .content("{}")) // missing required fields |
| 88 | .andExpect(status().isBadRequest()) |
| 89 | .andExpect(jsonPath("$.success").value(false)) |
| 90 | .andExpect(jsonPath("$.error.code").value("VALIDATION_FAILED")); |
| 91 | } |
| 92 | } |
| 93 | ``` |
| 94 | |
| 95 | ## Slice Tests — Repositories (@DataJpaTest) |
| 96 | |
| 97 | ```java |
| 98 | @DataJpaTest |
| 99 | @AutoConfigureTestDatabase(replace = Replace.NONE) // use real DB (Testcontainers) |
| 100 | @Import(TestcontainersConfig.class) |
| 101 | class OrderRepositoryTest { |
| 102 | |
| 103 | @Autowired OrderRepository orderRepository; |
| 104 | |
| 105 | @Test |
| 106 | void findByStatus_shouldReturnMatchingOrders() { |
| 107 | // Given |
| 108 | var order1 = orderRepository.save(Order.create("a@example.com")); |
| 109 | var order2 = orderRepository.save(Order.create("b@example.com")); |
| 110 | order2.ship(); // change status |
| 111 | orderRepository.save(order2); |
| 112 | |
| 113 | // When |
| 114 | List<Order> pending = orderRepository.findByStatus(OrderStatus.PENDING, Pageable.unpaged()).getContent(); |
| 115 | |
| 116 | // Then |
| 117 | assertThat(pending).hasSize(1); |
| 118 | assertThat(pending.get(0).getCustomerEmail()).isEqualTo("a@example.com"); |
| 119 | } |
| 120 | } |
| 121 | ``` |
| 122 | |
| 123 | ## Integration Tests — Testcontainers |
| 124 | |
| 125 | ```java |
| 126 | // Shared config — reuse container across tests |
| 127 | @TestConfiguration(proxyBeanMethods = false) |
| 128 | public class TestcontainersConfig { |
| 129 | |
| 130 | @Bean |
| 131 | @ServiceConnection |
| 132 | PostgreSQLContainer<?> postgresContainer() { |
| 133 | return new PostgreSQLContainer<>("postgres:16-alpine"); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Full integration test |
| 138 | @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) |
| 139 | @Import(TestcontainersConfig.class) |
| 140 | class OrderIntegrationTest { |
| 141 | |
| 142 | @Autowired TestRestTemplate restTemplate; |
| 143 | @Autowired OrderRepository orderRepository; |
| 144 | |
| 145 | @Test |
| 146 | void createAndRetrieveOrder_endToEnd() { |
| 147 | // Create |
| 148 | var creat |