
package com.example.tdd_study.product;
public class ProductService {
public void register(String name, int price) {
// 내용은 아직 비워둡니다
}
}
@Test
@DisplayName("상품 등록이 성공해야 한다")
void 상품_등록_성공() {
// Given: 테스트를 위한 준비
String name = "아메리카노";
int price = 4500;
ProductService productService = new ProductService();
// When: 실제 테스트할 동작 수행
productService.register(name, price);
// 검증(Assert) 로직이 아직 없습니다!
}package com.example.tdd_study.product;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
class ProductServiceTest {
@Test
@DisplayName("상품 등록이 성공해야 한다")
void 상품_등록_성공() {
// Given: 테스트를 위한 준비
String name = "아메리카노";
int price = 4500;
ProductService productService = new ProductService();
// When: 실제 테스트할 동작 수행
productService.register(name, price);
// 등록된 상품을 가져와서 검증
Product product = productService.findProduct();
assertThat(product.getName()).isEqualTo("아메리카노");
}
}
package com.example.tdd_study.product;
public class Product {
private String name;
private int price;
public Product(String name, int price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
}package com.example.tdd_study.product;
public class ProductService {
private Product product;
// 상품 등록 시, 단순히 멤버 변수에 저장
public void register(String name, int price) {
this.product = new Product(name, price);
}
// 등록된 상품을 반환
public Product findProduct() {
return this.product;
}
}다음 시간에는 이 코드를 전문가답게 다듬는 리팩토링(Refactoring) 과정을 진행하겠습니다. 이때 진짜 설계의 마법이 일어납니다.