DevKim

[02] ์ฃผ๋ฌธ ๋„๋ฉ”์ธ ๊ฐœ๋ฐœ ๋ณธ๋ฌธ

Spring Boot

[02] ์ฃผ๋ฌธ ๋„๋ฉ”์ธ ๊ฐœ๋ฐœ

on_doing 2021. 7. 8. 22:46
728x90

๐Ÿƒ ๊ตฌํ˜„ ๊ธฐ๋Šฅ ๐Ÿƒ

- ์ƒํ’ˆ ์ฃผ๋ฌธ

- ์ฃผ๋ฌธ ๋‚ด์—ญ ์กฐํšŒ

- ์ฃผ๋ฌธ ์ทจ์†Œ


[ ์ฃผ๋ฌธ ๋„๋ฉ”์ธ ์ƒ์„ฑ ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€ ]

๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๊ฐ€์žฅ ๋ณต์žกํ•œ ์ฃผ๋ฌธ ๋„๋ฉ”์ธ ๋ถ€๋ถ„์ด๋‹ค.

 

๐Ÿ“Œ 01. ์ฃผ๋ฌธ ์ƒ์„ฑ ๋ฉ”์„œ๋“œ ๋งŒ๋“ค๊ธฐ

์ƒ์„ฑ์ž๋กœ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ, ์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•จ์œผ๋กœ์จ ๋” ํšจ๊ณผ์ ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์งค ์ˆ˜ ์žˆ๋‹ค.

์ •์  ํŒฉํ† ๋ฆฌ๋Š” ๊ทธ ์œ ๋ช…ํ•œ ์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ” ์ฒซ์žฅ์— ๋‚˜์˜ค๋Š” ์ด์•ผ๊ธฐ์ด๋‹ค. ์ž์„ธํ•œ๊ฑด ๋‹ค์Œ์— ์ •๋ฆฌ๋ฅผ ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

 

๋„๋ฉ”์ธ ์ฃผ๋„ ๊ฐœ๋ฐœ์œผ๋กœ ์‹ค์Šต์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๊ธฐ์—, 

ํ•ด๋‹น ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๋ถ€๋ถ„์—์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ํ•ด๊ฒฐํ•œ๋‹ค.

@Entity
@Getter
@Setter
@Table(name = "orders")
public class Order {

    @Id
    @GeneratedValue
    @Column(name = "order_id")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "member_id")
    private Member member;

    @OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
    private List<OrderItem> orderItems = new ArrayList<>();

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "delivery_id")
    private Delivery delivery;

    private LocalDateTime orderDate; //์ฃผ๋ฌธ ์‹œ๊ฐ„

    @Enumerated(EnumType.STRING) //์ฃผ๋ฌธ ์ƒํƒœ [ORDER,CANCEL]
    private OderStatus status;

    /**
     * ์—ฐ๊ด€๊ด€๊ณ„ ํŽธ์˜ ๋ฉ”์„œ๋“œ (์–‘๋ฐฉํ–ฅ)
     */
    public void setMember(Member member) {
        this.member = member;
        member.getOrders().add(this);
    }

    public void addOrderItem(OrderItem orderItem) {
        orderItems.add(orderItem);
        orderItem.setOrder(this);

    }

    public void setDelivery(Delivery delivery) {
        this.delivery = delivery;
        delivery.setOrder(this);
    }

    /**
     * ์ฃผ๋ฌธ ์ƒ์„ฑ ๋ฉ”์„œ๋“œ
     * todo : ์ฃผ๋ฌธ ํšŒ์›, ๋ฐฐ์†ก ์ •๋ณด, ์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•˜์—ฌ ์ฃผ๋ฌธ ์ƒ์„ฑ
     */
    public static Order createOrder(Member member, Delivery delivery, OrderItem... orderItems) {
        Order order = new Order();
        order.setMember(member);
        order.setDelivery(delivery);
        for (OrderItem item : orderItems) {
            order.addOrderItem(item);
        }
        order.setStatus(OderStatus.ORDER);
        order.setOrderDate(LocalDateTime.now());
        return order;
    }

    ...

}

Setter๋Š” ์‚ฌ์‹ค์ƒ ๋นผ์ค˜์•ผํ•˜๊ณ , ๋ฐ์ดํ„ฐ๋„ Setter๋กœ ๋ฐ”๋กœ ๋ฐ”๊พธ๋ฉด ์•ˆ๋˜์ง€๋งŒ.. ์—ฐ์Šต์ด๋‹ˆ๊นŒ..ใ…Žใ…Ž

์ฃผ๋ฌธํšŒ์›,๋ฐฐ์†ก์ •๋ณด, ์ƒํ’ˆ๋ช…์„ ์ž…๋ ฅํ•˜์—ฌ ์ฃผ๋ฌธ์„ ์ƒ์„ฑํ•ด์ฃผ์—ˆ๋‹ค.


[ ์ฃผ๋ฌธ ๋„๋ฉ”์ธ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ถ”๊ฐ€ ]

๐Ÿ“Œ 1. ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง

- ์ฃผ๋ฌธ์ด ์ทจ์†Œ ๋˜์—ˆ์„ ๋•Œ -> ์žฌ๊ณ  ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€ํ•ด์•ผํ•จ

- ๋ฐฐ์†ก ์ƒํƒœ๊ฐ€ ์ด๋ฏธ ๋ฐฐ์†ก์ค‘์ด๋ผ๋ฉด, ์ทจ์†Œ๋Š” ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰

- ๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ์ทจ์†Œ ์ƒํƒœ๋กœ ๋ฐ”๊ฟ”์ฃผ๊ณ , orderItem ์ชฝ์— cancel ๋ฉ”์†Œ๋“œ๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ฆ๊ฐ€์‹œ์ผœ์ค€๋‹ค.

 

๐Ÿ“Œ 2. ์กฐํšŒ ๋กœ์ง

- ์ „์ฒด ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ ์กฐํšŒํ•˜๊ธฐ

@Entity
@Getter
@Setter
@Table(name = "orders")
public class Order {

   ...

    /**
     * ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง
     * todo : ์ฃผ๋ฌธ ์ทจ์†Œ -> ์žฌ๊ณ  ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€
     */
    public void cancel(Order order) {
        //์ด๋ฏธ ๋ฐฐ์†ก์™„๋ฃŒ
        if (delivery.getStatus() == DeliveryStatus.COMP) {
            throw new IllegalStateException("์ด๋ฏธ ๋ฐฐ์†ก์™„๋ฃŒ๋œ ์ƒํ’ˆ์€ ์ทจ์†Œํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
        }
        this.setStatus(OderStatus.CANCEL);

        for (OrderItem orderItem : orderItems) {
            orderItem.cancel();
        }

    }

    /**
     * ์กฐํšŒ ๋กœ์ง
     * todo : ์ „์ฒด ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ ์กฐํšŒ
     */
    public int getTotalPrice() {
        int totalPrice = 0;
        for (OrderItem orderItem : orderItems) {
            totalPrice += orderItem.getTotalPrice();

        }
        return totalPrice;
    }
    
    //==stream,lambda ์‚ฌ์šฉ์‹œ==//

//public int getTotalPrice() {
//        int totalPrice = orderItems.stream()
//                .mapToInt(OrderItem::getTotalPrice)
//                .sum();
//        return totalPrice;
//    }

}

/**
* ๋น„์Šค๋‹ˆ์Šค ๋กœ์ง
* todo: ์ฃผ๋ฌธ ์ทจ์†Œ์‹œ ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰๋งŒํผ ์žฌ๊ณ  ๋Š˜๋ฆฌ๊ธฐ
*/

public void cancel() {
	this.getItem().addStock(count);
}

 

์ด๊ฒƒ๋˜ํ•œ orderItem์— ์žˆ๋Š” ํ˜„์žฌ ์ œํ’ˆ์˜ ๊ฐ€๊ฒฉ๊ณผ ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰์„ ๊ณฑํ•ด์„œ ๊ฐ€์ ธ์˜จ๋‹ค.

/**
* todo : ์ „์ฒด ๊ฐ€๊ฒฉ
*/
public int getTotalPrice() {
     return orderPrice*count;
    }

[ ์ฃผ๋ฌธ์ƒํ’ˆ ์ƒ์„ฑ ๋ฉ”์„œ๋“œ ]

์ •์  ํŒฉํ† ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฃผ๋ฌธ์ƒํ’ˆ ์ƒ์„ฑ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

    /**
     * ์ƒ์„ฑ ๋ฉ”์„œ๋“œ - ์ฃผ๋ฌธ ํ•  ์•„์ดํ…œ, ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ, ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰
     */
    public static OrderItem createOrderItem(Item item,int orderPrice,int count)
    {
        OrderItem orderItem = new OrderItem();
        orderItem.setItem(item);
        orderItem.setOrderPrice(orderPrice);
        orderItem.setCount(count);

        item.removeStock(count);
        return orderItem;
    }

 


[ ์ฃผ๋ฌธ Repository ]

์ฃผ๋ฌธ์—๋Š” ํšŒ์›์— ๋”ฐ๋ฅธ ์ฃผ๋ฌธ ์ƒํƒœ์™€ ์ฃผ๋ฌธ ์ƒํƒœ์— ๋”ฐ๋ผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๋Š” ๋ถ€๋ถ„์ด์–ด์„œ

๋™์  ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค. ๋™์ ์ฟผ๋ฆฌ๋Š” ์ผ๋‹จ JPA Criteria๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ๋กœ ๋‘๊ณ ,

Querydsl๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ถ€๋ถ„์€ todo๋กœ ๋‚จ๊ฒจ๋’€๋‹ค.

@Repository
@RequiredArgsConstructor
public class OrderRepository {

    private final EntityManager em;

    public void save(Order order)
    {
        em.persist(order);
    }

    //๋‹จ๊ฑด ์กฐํšŒ
    public Order findOne(Long id)
    {
        return em.find(Order.class,id);
    }

    /**
     * todo: ์ฃผ๋ฌธ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ (๋™์  ์ฟผ๋ฆฌ)
     */
}

[ ์ฃผ๋ฌธ Service ]

๐Ÿ“Œ 01. ์ฃผ๋ฌธํ•˜๊ธฐ

Step 1. ์ฃผ๋ฌธํ•œ member๊ฐ€ ๋ˆ„๊ตฐ์ง€ ์กฐํšŒ

Step 2. ๋ฐฐ์†ก ์ •๋ณด ์ƒ์„ฑ (์ƒํƒœ๋„ ๋ณ€๊ฒฝ)

Step 3. ์ฃผ๋ฌธํ•œ ์ƒํ’ˆ ์ƒ์„ฑ (์—ฌ๋Ÿฌ๊ฐœ)

Step 4. ์ฃผ๋ฌธ ์ƒ์„ฑ

Step 5. ์ฃผ๋ฌธ ์ €์žฅ

@RequiredArgsConstructor
@Service
@Transactional
public class OrderService {

    private final MemberRepository memberRepository;
    private final ItemRepository itemRepository;
    private final OrderRepository orderRepository;

    /**
     * todo : ์ฃผ๋ฌธํ•˜๊ธฐ
     * input : ์ฃผ๋ฌธํ•œ memberId, ์ฃผ๋ฌธํ•œ ์ƒํ’ˆ, ์ƒํ’ˆ ๊ฐฏ์ˆ˜
     * 1. ์ฃผ๋ฌธํ•œ member๊ฐ€ ๋ˆ„๊ตฐ์ง€ ์กฐํšŒ
     * 2. ๋ฐฐ์†ก ์ •๋ณด ์ƒ์„ฑ
     * 3. ์ฃผ๋ฌธํ•œ ์ƒํ’ˆ์ƒ์„ฑ (์—ฌ๋Ÿฌ๊ฐœ)
     * 4. ์ฃผ๋ฌธ ์ƒ์„ฑ
     * 5. ์ฃผ๋ฌธ ์ €์žฅ
     */
    public Long Order(Long memberId, Long ItemId, int count)
    {
        //์ฃผ๋ฌธํ•œ ์‚ฌ๋žŒ ์ฐพ๊ธฐ
        Member member= memberRepository.findOne(memberId);

        //์ฃผ๋ฌธํ•œ ์ƒํ’ˆ ์ฐพ๊ธฐ
        Item item = itemRepository.findOne(ItemId);

        //๋ฐฐ์†ก์ •๋ณด ์ƒ์„ฑ
        Delivery delivery =new Delivery();
        delivery.setAddress(member.getAddress());
        delivery.setStatus(DeliveryStatus.READY);

        //์ฃผ๋ฌธํ•œ ์ƒํ’ˆ์ƒ์„ฑ
        OrderItem orderItem = OrderItem.createOrderItem(item, item.getPrice(), count);

        //์ฃผ๋ฌธ ์ƒ์„ฑ -๋ฉค๋ฒ„,๋ฐฐ์†ก์ •๋ณด,์ฃผ๋ฌธํ•  ์ƒํ’ˆ
        Order order= Order.createOrder(member, delivery,orderItem);

        //์ฃผ๋ฌธ ์ €์žฅ
        orderRepository.save(order);

        return order.getId();
    }
    ...
    
    }

์ด๋•Œ, ์ฃผ๋ฌธ๊ณผ ์ฃผ๋ฌธ์ƒํ’ˆ์€ ์ƒ์„ฑ ๋ฉ”์„œ๋“œ๋ฅผ ๋”ฐ๋กœ ๋งŒ๋“ค์–ด๋†จ๊ธฐ ๋•Œ๋ฌธ์—

๋ฐ–์—์„œ ๋ˆ„๊ฐ€ new๋กœ ์ƒ์„ฑํ•˜์ง€ ์•Š๋„๋ก ์ฃผ๋ฌธ๊ณผ ์ฃผ๋ฌธ์ƒํ’ˆ์˜ ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ protected ๋กœ ์ถ”๊ฐ€๋กœ ์„ค์ •ํ•ด์ฃผ์—ˆ๋‹ค.

 

๐Ÿ“Œ02. ์ฃผ๋ฌธ ์ทจ์†Œ

step 1. ์ทจ์†Œ๋ฅผ ๋„ฃ์€ ํ•ด๋‹น ์ฃผ๋ฌธ ์ฐพ๊ธฐ

step 2. ์ฃผ๋ฌธ์ทจ์†Œ์— ๋”ฐ๋ฅธ ์žฌ๊ณ  ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€

/**
* todo : ์ฃผ๋ฌธ ์ทจ์†Œ
* Input : ์ทจ์†Œํ•  ์ฃผ๋ฌธ id
*/
public void CancelOrder(Long OrderId)
{
	Order order = orderRepository.findOne(OrderId);
	order.cancel();
}

๐Ÿ“Œ03. ์ฃผ๋ฌธ ๊ฒ€์ƒ‰

//Querydsl๋กœ ๋ฆฌํŒฉํ† ๋ง ์˜ˆ์ •


์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ๋Œ€๋ถ€๋ถ„ ์—”ํ‹ฐํ‹ฐ์— ์žˆ๋‹ค.

์„œ๋น„์Šค ๊ณ„์ธต์€ ๋‹จ์ˆœํžˆ ํ•„์š”ํ•œ ์—ญํ• ๋“ค์„ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ๋งŒํ•œ๋‹ค.

JPA๊ฐ™์€ ORM์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ๋„๋ฉ”์ธ ๋ชจ๋ธ ํŒจํ„ด์„ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ผ๋ฐ˜์ ์ด๋ผ๊ณ ํ•œ๋‹ค.

<-> ํŠธ๋žœ์žญ์…˜ ์Šคํฌ๋ฆฝํŠธ ํŒจํ„ด (์ด๊ฒŒ ๋” ์œ ์ง€๋ณด์ˆ˜ํ•˜๋Š”๋ฐ ์ข‹์„ ๋•Œ๋„ ์žˆ์Œ)


[ ์ฃผ๋ฌธ ๊ธฐ๋Šฅ ํ…Œ์ŠคํŠธ ]

- ์ƒํ’ˆ ์ฃผ๋ฌธ ์„ฑ๊ณต์—ฌ๋ถ€

- ์ฃผ๋ฌธ ์ทจ์†Œ ์„ฑ๊ณต์—ฌ๋ถ€

- ์ƒํ’ˆ์„ ์ฃผ๋ฌธํ•  ๋•Œ ์žฌ๊ณ  ์ˆ˜๋Ÿ‰์„ ์ดˆ๊ณผํ•˜๋ฉด ์•ˆ๋จ

 

 

๐Ÿ“Œ 01. ์ƒํ’ˆ ์ฃผ๋ฌธ ์„ฑ๊ณต์—ฌ๋ถ€

- ์ฃผ๋ฌธ ํ›„ ์ฃผ๋ฌธ์ƒํƒœ๊ฐ€ ORDER๋กœ ๋ณ€๊ฒฝ๋˜์—ˆ๋Š”์ง€

- ์ฃผ๋ฌธํ•œ ์ƒํ’ˆ์˜ ์ข…๋ฅ˜์˜ ์ˆ˜ ํ™•์ธ

- ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ = ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰*์ฃผ๋ฌธ ๊ฐ€๊ฒฉ์ธ์ง€

- ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰๋งŒํผ ์žฌ๊ณ  ๊ฐ์†Œํ–ˆ๋Š”์ง€

	/**
     * todo: ์ƒํ’ˆ ์ฃผ๋ฌธํ•˜๊ธฐ ์„ฑ๊ณต์—ฌ๋ถ€
     * member,Item,Count
     */
    @Test
    public void ์ƒํ’ˆ_์ฃผ๋ฌธํ•˜๊ธฐ() throws Exception{
        //given
        //๋ฉค๋ฒ„ ์ƒ์„ฑ
        Member member=createMember();
        //์ƒํ’ˆ ์ƒ์„ฑ
        Item item=createItem();
        // ์ฃผ๋ฌธํ•œ ์ƒํ’ˆ ๊ฐฏ์ˆ˜
        int count=2;

        //when
        // ์ฃผ๋ฌธ์ƒ์„ฑ
        Long OrderId = orderService.Order(member.getId(), item.getId(), count);

        //then
        Order order=orderRepository.findOne(OrderId);
        /**
         * ์ฃผ๋ฌธ ์ƒํƒœ :ORDER
         * ์ฃผ๋ฌธํ•œ ์ƒํ’ˆ ์ข…๋ฅ˜์˜ ์ˆ˜ :1
         * ์ฃผ๋ฌธ ๊ฐ€๊ฒฉ = ์ฃผ๋ฌธ์ˆ˜๋Ÿ‰*์ฃผ๋ฌธ ๊ฐ€๊ฒฉ
         * ์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰๋งŒํผ ์žฌ๊ณ  ๊ฐ์†Œ
         */
        assertEquals("์ฃผ๋ฌธ ์ƒํƒœ๋Š” Status", OrderStatus.ORDER,order.getStatus());
        assertEquals("์ฃผ๋ฌธ ์ƒํ’ˆ ์ข…๋ฅ˜์˜ ์ˆ˜๋Š” 1๊ฐœ",1,order.getOrderItems().size());
        assertEquals("์ฃผ๋ฌธ ๊ฐ€๊ฒฉ์€ ์ฃผ๋ฌธ์ˆ˜๋Ÿ‰*์ฃผ๋ฌธ๊ฐ€๊ฒฉ",2*10000,order.getTotalPrice());
        assertEquals("์ฃผ๋ฌธ ์ˆ˜๋Ÿ‰๋งŒํผ ์žฌ๊ณ  ๊ฐ์†Œ",98,item.getStockQuantity());

    }

๐Ÿ“Œ02. ์ฃผ๋ฌธ์ทจ์†Œ ์„ฑ๊ณต์—ฌ๋ถ€

- ์ฃผ๋ฌธ ์ทจ์†Œ์‹œ ์ƒํƒœ๋Š” CANCEL

- ์ฃผ๋ฌธ ์ทจ์†Œ์‹œ ์žฌ๊ณ  ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€

	@Test
    public void ์ฃผ๋ฌธ์ทจ์†Œ() throws Exception{
        //given
        //๋ฉค๋ฒ„ ์ƒ์„ฑ
        Member member=createMember();
        //์ƒํ’ˆ ์ƒ์„ฑ
        Item item=createItem();
        // ์ฃผ๋ฌธํ•œ ์ƒํ’ˆ ๊ฐฏ์ˆ˜
        int count=2;
        // ์ฃผ๋ฌธ์ƒ์„ฑ
        Long OrderId = orderService.Order(member.getId(), item.getId(), count);

        //when
        orderService.CancelOrder(OrderId);

        //then
        /**
         * ์ฃผ๋ฌธ ์ทจ์†Œ์‹œ ์ƒํƒœ๋Š” CANCEL
         * ์ฃผ๋ฌธ ์ทจ์†Œ์‹œ ์žฌ๊ณ  ์ˆ˜๋Ÿ‰ ์ฆ๊ฐ€
         */
        Order order =orderRepository.findOne(OrderId);
        assertEquals("์ฃผ๋ฌธ ์ทจ์†Œ์‹œ ์ƒํƒœ๋Š” CANCEL",OrderStatus.CANCEL,order.getStatus());
        assertEquals("์ฃผ๋ฌธ ์ทจ์†Œ์‹œ ์ทจ์†Œ๋œ ์ƒํ’ˆ์˜ ์žฌ๊ณ ๋Š” ์ฆ๊ฐ€",100,item.getStockQuantity());

    }

๐Ÿ“Œ03. ์žฌ๊ณ ๋ณด๋‹ค ๋” ๋งŽ์ด ์ฃผ๋ฌธํ–ˆ์„ ๋•Œ

- ์˜ˆ์ƒ๋Œ€๋กœ ์ด์ „์— ์ƒ์„ฑํ•ด๋‘” NotEnoughStockException์ด ํ„ฐ์ ธ์•ผํ•œ๋‹ค.

	@Test(expected = NotEnoughStockException.class)
    public void ์ƒํ’ˆ์ฃผ๋ฌธ_์žฌ๊ณ ์ˆ˜๋Ÿ‰์ดˆ๊ณผ() throws Exception{
        //given

        //๋ฉค๋ฒ„ ์ƒ์„ฑ
        Member member=createMember();

        //์ƒํ’ˆ ์ƒ์„ฑ
        Item item=createItem();

        // ์ดˆ๊ณผ๋กœ ์ƒํ’ˆ ์ฃผ๋ฌธ
        int count=101;

        //when
        // ์ฃผ๋ฌธ์ƒ์„ฑ
        Long OrderId = orderService.Order(member.getId(), item.getId(), count);

        //then
        fail("์žฌ๊ณ  ์ˆ˜๋Ÿ‰ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•ด์•ผํ•œ๋‹ค!!!");


    }
728x90
Comments