This Claude skill serves as a specialized Testing Expert for Spring Boot applications, focusing on robust Integration and E2E testing strategies. It optimizes the development lifecycle by enforcing realistic HTTP validation via TestRestTemplate, implementing reusable data management with Gradle testFixtures, and ensuring architectural integrity through ArchUnit. It is designed to help developers build high-coverage, maintainable test suites within Hexagonal Architecture environments.
Use Cases
Generating end-to-end (E2E) integration tests using TestRestTemplate and TestContainers to validate real-world HTTP interactions and database persistence.
Establishing a modular test data strategy by creating reusable Fixture classes across Domain, Application, and Adapter layers using the Gradle testFixtures plugin.
Automating complex test data setups for specific features using @Sql scripts for DML operations while maintaining test isolation with @Transactional rollbacks.
Enforcing architectural constraints and dependency rules (such as Hexagonal Architecture boundaries) through automated ArchUnit test generation and verification.
HTTP Request (TestRestTemplate)
↓
Controller (REST API Layer)
↓
UseCase (Application Layer)
↓
Repository (Persistence Layer)
↓
Real Database (MySQL via TestContainers)
↓
HTTP Response
단위 테스트 vs 통합 테스트
항목
단위 테스트
통합 테스트
범위
하나의 클래스/메서드
전체 레이어 통합
의존성
Mock/Stub
실제 Bean + 실제 DB
속도
빠름 (ms)
느림 (초)
테스트
@DataJpaTest, @WebMvcTest
@SpringBootTest
HTTP
MockMvc (가짜)
TestRestTemplate (실제)
DB
H2 (인메모리)
MySQL (실제)
Flyway vs @Sql 구분 (핵심!)
항목
Flyway
@Sql
목적
DB 스키마 버전 관리
테스트 데이터 삽입
역할
DDL (CREATE TABLE)
DML (INSERT)
실행 시점
앱 시작 시 (1번)
각 테스트 메서드 전
파일 위치
db/migration/
src/test/resources/sql/
운영 사용
✅ 사용
❌ 테스트 전용
코드 템플릿
1. Integration Test
package com.ryuqq.bootstrap;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
@Testcontainers
@Transactional
@DisplayName("Order 통합 테스트")
class OrderIntegrationTest {
@Container
static MySQLContainer<?> mysql = new MySQLContainer<>("mysql:15-alpine")
.withDatabaseName("test")
.withUsername("test")
.withPassword("test");
@Autowired
private TestRestTemplate restTemplate;
@Test
@Sql("/sql/orders-test-data.sql")
@DisplayName("E2E - 주문 생성")
void shouldCreateOrder() {
// Given
PlaceOrderRequest request = new PlaceOrderRequest(1L, 100L, 10);
// When
ResponseEntity<ApiResponse<OrderResponse>> response = restTemplate.exchange(
"/api/v1/orders",
HttpMethod.POST,
new HttpEntity<>(request),
new ParameterizedTypeReference<>() {}
);
// Then
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody()).isNotNull();
assertThat(response.getBody().getData().orderId()).isNotNull();
}
@Test
@Sql("/sql/orders-test-data.sql")
@DisplayName("E2E - 주문 조회")
void shouldGetOrder() {
// Given
Long orderId = 100L;
// When
ResponseEntity<ApiResponse<OrderResponse>> response = restTemplate.exchange(
"/api/v1/orders/{orderId}",
HttpMethod.GET,
null,
new ParameterizedTypeReference<>() {},
orderId
);
// Then
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody().getData().orderId()).isEqualTo(orderId);
}
}
핵심 규칙:
@SpringBootTest(webEnvironment = RANDOM_PORT) 필수
TestRestTemplate 주입 (MockMvc 금지)
@Sql 로 테스트 데이터 삽입 (DML만)
@Transactional 롤백으로 테스트 격리
@Testcontainers + MySQL 실제 DB
2. Domain Fixture (testFixtures 소스셋)
package com.ryuqq.fixture.domain;
import com.ryuqq.domain.order.aggregate.Order;
import com.ryuqq.domain.order.vo.OrderId;
import com.ryuqq.domain.order.vo.OrderStatus;
import com.ryuqq.domain.order.vo.Money;
import java.math.BigDecimal;
/**
* Order Domain 객체 Test Fixture
*
* <p>모든 레이어에서 재사용 가능한 Domain 객체 생성 유틸리티</p>
*/
public final class OrderFixture {
private OrderFixture() {
throw new AssertionError("Utility class - do not instantiate");
}
/**
* 신규 Order (ID 미할당)
*/
public static Order defaultNewOrder() {
return Order.forNew(
new CustomerId(1L),
Money.of(BigDecimal.valueOf(50000))
);
}
/**
* 기존 Order (ID 할당됨, 저장된 상태)
*/
public static Order defaultExistingOrder() {
return Order.forExisting(
OrderId.of(1L),
new CustomerId(1L),
Money.of(BigDecimal.valueOf(50000)),
OrderStatus.PLACED
);
}
/**
* 확정된 Order
*/
public static Order confirmedOrder() {
Order order = defaultExistingOrder();
order.confirm();
return order;
}
/**
* 취소된 Order
*/
public static Order canceledOrder() {
Order order = defaultExistingOrder();
order.cancel();
return order;
}
/**
* 커스텀 Order 빌더
*/
public static Order customOrder(Long id, BigDecimal amount, OrderStatus status) {
return Order.forExisting(
OrderId.of(id),
new CustomerId(1L),
Money.of(amount),
status
);
}
}
package com.ryuqq.fixture.application.command;
import com.ryuqq.application.order.dto.command.PlaceOrderCommand;
import java.math.BigDecimal;
/**
* PlaceOrderCommand DTO Test Fixture
*/
public final class PlaceOrderCommandFixture {
private PlaceOrderCommandFixture() {
throw new AssertionError("Utility class - do not instantiate");
}
public static PlaceOrderCommand defaultCommand() {
return new PlaceOrderCommand(
1L,
BigDecimal.valueOf(50000)
);
}
public static PlaceOrderCommand customCommand(Long customerId, BigDecimal amount) {
return new PlaceOrderCommand(customerId, amount);
}
}
4. Request Fixture (REST API)
package com.ryuqq.fixture.adapter.rest;
import com.ryuqq.adapter.in.rest.order.dto.PlaceOrderRequest;
import java.math.BigDecimal;
import java.util.List;
/**
* REST API Request DTO Test Fixture
*/
public final class OrderRequestFixture {
private OrderRequestFixture() {
throw new AssertionError("Utility class - do not instantiate");
}
public static PlaceOrderRequest validCreateRequest() {
return new PlaceOrderRequest(
1L,
List.of(OrderItemRequestFixture.validItemRequest())
);
}
public static PlaceOrderRequest invalidRequest_EmptyItems() {
return new PlaceOrderRequest(1L, List.of());
}
public static PlaceOrderRequest invalidRequest_NullCustomerId() {
return new PlaceOrderRequest(
null,
List.of(OrderItemRequestFixture.validItemRequest())
);
}
}
5. @Sql 테스트 데이터 (DML만!)
-- src/test/resources/sql/orders-test-data.sql
-- 클린업 (FK 역순)
DELETE FROM order_items;
DELETE FROM orders;
DELETE FROM customers;
-- 테스트 데이터 삽입 (FK 순서)
INSERT INTO customers (customer_id, name, email)
OVERRIDING SYSTEM VALUE
VALUES (1, 'Alice', 'alice@example.com');
INSERT INTO customers (customer_id, name, email)
OVERRIDING SYSTEM VALUE
VALUES (2, 'Bob', 'bob@example.com');
INSERT INTO orders (order_id, customer_id, status, total_amount, order_date)
OVERRIDING SYSTEM VALUE
VALUES (100, 1, 'PENDING', 10000, '2024-01-01');
INSERT INTO orders (order_id, customer_id, status, total_amount, order_date)
OVERRIDING SYSTEM VALUE
VALUES (101, 1, 'CONFIRMED', 20000, '2024-01-02');
INSERT INTO order_items (order_item_id, order_id, product_id, quantity, price)
OVERRIDING SYSTEM VALUE
VALUES (1000, 100, 200, 2, 5000);
핵심 규칙:
DDL 금지: CREATE TABLE, ALTER TABLE 절대 금지 (Flyway 책임)
DML만: INSERT, UPDATE, DELETE만 허용
클린업 먼저: DELETE → INSERT 순서
FK 순서: 부모 먼저, 자식 나중
6. Gradle build.gradle 설정
// domain/build.gradle
plugins {
id 'java-library'
id 'java-test-fixtures' // ⭐ testFixtures 활성화
}
dependencies {
// Test
testImplementation libs.junit.jupiter
testImplementation libs.archunit.junit5
// Test Fixtures - Domain은 순수 Java만
// NO external dependencies
}
// application/build.gradle
plugins {
id 'java-library'
id 'java-test-fixtures'
}
dependencies {
api project(':domain')
// Test
testImplementation libs.spring.boot.starter.test
testImplementation testFixtures(project(':domain')) // ⭐ Domain Fixture
// Test Fixtures
testFixturesApi project(':domain')
testFixturesApi testFixtures(project(':domain'))
}
1. TestContainers 시작
└─ Docker → MySQL 컨테이너 시작
2. Spring Boot 시작 (@SpringBootTest)
└─ 전체 Bean 로딩
3. Flyway 자동 실행 (spring.flyway.enabled=true)
└─ V1 → V2 → V3 ... (테이블 생성)
4. @Sql 실행 (각 테스트 메서드 전)
└─ INSERT 테스트 데이터
5. 테스트 메서드 실행
└─ TestRestTemplate → Controller → UseCase → Repository → DB
6. @Transactional 롤백
└─ @Sql 데이터 자동 삭제
7. 다음 테스트 메서드 (4~6 반복)
8. TestContainers 종료
└─ MySQL 컨테이너 삭제