Testing patterns, pyramid, TDD
This skill inherits all available tools. When active, it can use any tool Claude has access to.
/\
/ \ E2E Tests (Few, Slow, Expensive)
/----\
/ \ Integration Tests (Some)
/--------\
/ \ Unit Tests (Many, Fast, Cheap)
/------------\
@Test
void shouldCalculateTotalWithDiscount() {
// Arrange - Set up test data
Order order = new Order();
order.addItem(new Item("SKU-1", 100.00));
order.setDiscountPercent(10);
// Act - Execute the code
double total = order.calculateTotal();
// Assert - Verify results
assertThat(total).isEqualTo(90.00);
}
// Pattern: should_[expectedBehavior]_when_[condition]
void shouldReturnEmptyList_whenNoOrdersExist() { }
void shouldThrowException_whenOrderIdIsNull() { }
void shouldCalculateCorrectTotal_whenDiscountApplied() { }
@Mock
private OrderRepository orderRepository;
@Mock
private PaymentService paymentService;
@InjectMocks
private OrderService orderService;
@Test
void shouldProcessOrder() {
when(orderRepository.findById(1L))
.thenReturn(Optional.of(testOrder));
when(paymentService.charge(any()))
.thenReturn(PaymentResult.success());
orderService.processOrder(1L);
verify(paymentService).charge(any());
}
Order testOrder = OrderBuilder.anOrder()
.withId(1L)
.withStatus(OrderStatus.PENDING)
.withItems(List.of(testItem))
.build();
@BeforeEach
void setUp() {
testCustomer = CustomerFixtures.aValidCustomer();
testOrder = OrderFixtures.aPendingOrder(testCustomer);
}
| Type | Target |
|---|---|
| Line Coverage | > 80% |
| Branch Coverage | > 70% |
| Critical Paths | 100% |
@Test
void shouldProcessAsync() {
CompletableFuture<Result> future = service.processAsync(input);
Result result = future.get(5, TimeUnit.SECONDS);
assertThat(result.isSuccess()).isTrue();
}
@Test
void shouldCompleteWorkflow() {
WorkflowClient client = testEnvironment.getWorkflowClient();
MyWorkflow workflow = client.newWorkflowStub(MyWorkflow.class);
workflow.execute(input);
assertThat(workflow.getState()).isEqualTo(WorkflowState.COMPLETED);
}