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 27, 2024
1 parent dcaf15a commit 3f30f76
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
2 changes: 2 additions & 0 deletions product-order-service/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ final UpdateProductRequest request = new UpdateProductRequest(name, price, disco
**스프링부트 테스트로 전환하기**

> [스프링부트 테스트로 전환하기]()
**API 테스트로 전환하기**

**JPA 적용**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@
import com.example.productorderservice.product.Product;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/orders")
class OrderService {
public class OrderService {
private final OrderPort orderPort;

OrderService(final OrderPort orderPort) {
this.orderPort = orderPort;
}

@PostMapping
@Transactional
public ResponseEntity<Void> createOrder(@RequestBody final CreateOrderRequest request) {
final Product product = orderPort.getProductById(request.productId());

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

import org.springframework.stereotype.Component;

@Component
public class ConsolePaymentGateway implements PaymentGateway {
@Override
public void excute(final int totalPrice, final String cardNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.example.productorderservice.order.Order;
import com.example.productorderservice.product.DiscountPolicy;
import com.example.productorderservice.product.Product;
import org.springframework.stereotype.Component;

@Component
class PaymentAdapter implements PaymentPort {
private final PaymentGateway paymentGateway;
private final PaymentRepository paymentRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.example.productorderservice.payment;

import org.springframework.stereotype.Repository;

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

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

import com.example.productorderservice.order.Order;
import org.springframework.stereotype.Component;

@Component
class PaymentService {
private final PaymentPort paymentPort;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
package com.example.productorderservice.payment;

import org.junit.jupiter.api.BeforeEach;
import com.example.productorderservice.order.OrderService;
import com.example.productorderservice.order.OrderSteps;
import com.example.productorderservice.product.ProductService;
import com.example.productorderservice.product.ProductSteps;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class PaymentServiceTest {
private PaymentService paymentService;
private PaymentPort paymentPort;
@Autowired
private ProductService productService;

@BeforeEach
void setUp() {
final PaymentGateway paymentGateway = new ConsolePaymentGateway();
final PaymentRepository paymentRepository = new PaymentRepository();
paymentPort = new PaymentAdapter(paymentGateway, paymentRepository);
paymentService = new PaymentService(paymentPort);
}
@Autowired
private OrderService orderService;

@Autowired
private PaymentService paymentService;

@Test
void 상품주문() {
productService.addProduct(ProductSteps.getAddProductRequest());
orderService.createOrder(OrderSteps.getCreateOrderRequest());
final PaymentRequest request = PaymentSteps.getPaymentRequest();

paymentService.payment(request);
Expand Down

0 comments on commit 3f30f76

Please sign in to comment.