Skip to content

Commit

Permalink
[Order API TDD] 스프링부트 테스트로 전환하기
Browse files Browse the repository at this point in the history
  • Loading branch information
jihunparkme committed Feb 26, 2024
1 parent 7ddd7a9 commit 6760bf2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 17 deletions.
12 changes: 8 additions & 4 deletions product-order-service/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# POJO 상품 등록 기능 구현하기
## POJO 상품 등록 기능 구현하기

## Java TDD
**Java TDD**

(1) 필요한 서비스 호출
```java
Expand All @@ -23,13 +23,17 @@ final AddProductRequest request = new AddProductRequest(name, price, discountPol
(5) 레파지포리 생성
- 테스트에서는 데이터를 인메모리(Map)로 관리

## Move Inner class for test to Upper level
.

테스트 코드에 작성된 inner class 들을 main class 로 이동
**Move Inner class for test to Upper level**

테스트 코드에 작성된 inner class 들을 main class 로 이동

## 스프링부트 테스트로 전환하기

순수 자바로 구현된 서비스를 스프링 빈으로 등록하고 스프링 부트 테스트로 동작하도록 전환
- service, adapter, repository 클래스에 @Component 선언
- 기존 테스트에 @SpringBootTest 선언 및 서비스는 @Autowired 로 주입

---

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.productorderservice.product;

import org.springframework.stereotype.Component;

@Component
class ProductAdapter implements ProductPort {
private final ProductRepository productRepository;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.example.productorderservice.product;

import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Repository
class ProductRepository {
private Map<Long, Product> persistence = new HashMap<>();
private Long sequence = 0L;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.example.productorderservice.product;

/**
* 04. ProductService 메서드 구현
*/
import org.springframework.stereotype.Component;

@Component
class ProductService {
private final ProductPort productPort;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.example.productorderservice.product;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class ProductServiceTest {

@Autowired
private ProductService productService;
private ProductPort productPort;
private ProductRepository productRepository;

@BeforeEach
void setUp() {
productRepository = new ProductRepository();
productPort = new ProductAdapter(productRepository);
productService = new ProductService(productPort);
}


@Test
void 상품등록() {
Expand Down

0 comments on commit 6760bf2

Please sign in to comment.