ProLesson 3.4Intermediate2 min readUpdated 31 October 2026
Testing & Observability in Microservices
Verify microservice boundaries with unit and slice tests, and enable production observability with Spring Boot Actuator.
A microservice is only as reliable as its automated test suite and production observability hooks.
In this lesson, we cover the testing pyramid for a single microservice—from fast unit tests to @WebMvcTest controller slices—and configure Spring Boot Actuator for Kubernetes production monitoring.
01Unit Testing the Domain Service (JUnit 5 & Mockito)
Unit tests should test pure business rules in milliseconds without starting a Spring application context:
java
package com.veltoryx.order.service;
import com.veltoryx.order.controller.dto.CreateOrderRequest;
import com.veltoryx.order.controller.dto.OrderResponse;
import com.veltoryx.order.model.entity.OrderEntity;
import com.veltoryx.order.model.enums.OrderStatus;
import com.veltoryx.order.repository.OrderRepository;
import com.veltoryx.order.service.impl.OrderServiceImpl;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock
private OrderRepository orderRepository;
@InjectMocks
private OrderServiceImpl orderService;
@Test
@DisplayName("Should successfully create order when request payload is valid")
void shouldCreateOrderSuccessfully() {
// Given
var item = new CreateOrderRequest.OrderItemDto("prod_1", 2, BigDecimal.valueOf(50.00));
var request = new CreateOrderRequest("cust_99", List.of(item));
var savedEntity = new OrderEntity();
savedEntity.setId(UUID.randomUUID());
savedEntity.setCustomerId("cust_99");
savedEntity.setTotalAmount(BigDecimal.valueOf(100.00));
savedEntity.setStatus(OrderStatus.SUBMITTED);
when(orderRepository.save(any(OrderEntity.class))).thenReturn(savedEntity);
// When
OrderResponse response = orderService.createOrder(request);
// Then
assertThat(response.customerId()).isEqualTo("cust_99");
assertThat(response.totalAmount()).isEqualByComparingTo("100.00");
assertThat(response.status()).isEqualTo(OrderStatus.SUBMITTED);
verify(orderRepository).save(any(OrderEntity.class));
}
}Loading the rest of the lesson…