TDD cycle and Testcontainers testing strategy - Red-Green-Refactor, concurrency tests, ArchUnit verification
I guide TDD (Test-Driven Development) practices and Testcontainers-based integration testing for this e-commerce backend. I ensure tests are reliable, repeatable, and catch concurrency issues.
Use this skill when:
@Test
void createOrder_whenStockAvailable_shouldSucceed() {
// Arrange
Product product = Product.builder()
.id(1L)
.name("Test Product")
.stock(10)
.price(BigDecimal.valueOf(10000))
.build();
// Act & Assert - This should FAIL first
assertThat(product.decreaseStock(5)).isTrue();
assertThat(product.getStock()).isEqualTo(5);
}
// Simple implementation to pass the test
public boolean decreaseStock(int quantity) {
if (this.stock >= quantity) {
this.stock -= quantity;
return true;
}
return false;
}
// Refactor to proper domain model
public record Stock(int quantity) {
public Stock decrease(int quantity) {
if (this.quantity < quantity) {
throw new DomainException(ErrorCode.INSUFFICIENT_STOCK);
}
return new Stock(this.quantity - quantity);
}
}