작성
·
549
0
package jpabook.jpashop.service;
import jpabook.jpashop.domain.Address;
import jpabook.jpashop.domain.Member;
import jpabook.jpashop.domain.Order;
import jpabook.jpashop.domain.OrderStatus;
import jpabook.jpashop.domain.item.Item;
import jpabook.jpashop.exception.NotEnoughStockException;
import jpabook.jpashop.repository.OrderRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.EnableMBeanExport;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import java.awt.print.Book;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class OrderServiceTest {
@Autowired EntityManager em;
@Autowired OrderService orderService;
@Autowired
OrderRepository orderRepository;
/**
* 상품 주문
*/
@Test
public void order() {
// given
Member member = createMember();
Book book = createBook("시골 JPA", 10000, 10);
int orderCount = 2;
// when
Long orderId = orderService.order(member.getId(), book.getId(), orderCount);
// then
Order getOrder = orderRepository.findOne(orderId);
assertEquals("상품 주문시 상태는 ORDER", OrderStatus.ORDER, getOrder.getStatus());
assertEquals("주문한 상품 종류 수가 정확해야 한다.", 1, getOrder.getOrderItems().size());
assertEquals("주문 가격은 가격 * 수량이다.", 1000 * orderCount, getOrder.getTotalPrice());
assertEquals("주문 수량만큼 재고가 줄어야 한다.", 8, book.getStockQuantity());
}
/**
* 주문취소
*/
@Test
public void cancelOrder() {
Member member = createMember();
Book item = createBook("시골 JPA", 10000, 10);
int orderCount = 2;
Long orderId = orderService.order(member.getId(), item.getId(), orderCount);
orderService.cancelOrder(orderId);
Order getOrder = orderRepository.findOne(orderId);
assertEquals("주문 취소시 상태는 CANCEL 이다.", OrderStatus.CANCEL, getOrder.getStatus());
assertEquals("주문이 취소된 상품은 그만큼 재고가 증가해야한다.", 10, item.getStockQuantity());
}
/**
* 재고수량 초과
*/
@Test(expected = NotEnoughStockException.class)
public void 상품주문_재고수량초과() throws Exception{
// given
Member member = createMember();
Item item = createBook("시골 JPA", 10000, 10);
int orderCount = 11;
// when
orderService.order(member.getId(), item.getId(), orderCount);
// then
fail("재고 수량 부족 예외가 발생해야 한다.");
}
private Book createBook(String name, int price, int stockQuantity) {
Book book = new Book();
book.setName(name);
book.setPrice(price);
book.setStockQuantity(stockQuantity);
em.persist(book);
return book;
}
private Member createMember() {
Member member = new Member();
member.setName("회원1");
member.setAddress(new Address("서울", "강가", "123-123"));
em.persist(member);
return member;
}
package jpabook.jpashop.domain.item;
import jpabook.jpashop.domain.Category;
import jpabook.jpashop.exception.NotEnoughStockException;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "dtype")
@Getter @Setter
public abstract class Item {
@Id
@GeneratedValue
@Column(name = "item_id")
private Long id;
private String name;
private int price;
private int stockQuantity;
@ManyToMany(mappedBy = "items")
private List<Category> categories = new ArrayList<>();
// == 비즈니스 로직 == //
// stockQuantity의 경우, Item 엔티티 내에만 있으므로 해당 클래스에서 비즈니스 로직을 설정(엔티티 주도 설계)
/**
* 재고 수량 증가*
* */
public void addStock(int quantity){
this.stockQuantity += quantity;
}
/**
* 재고 수량 감소*
* */
public void removeStock(int quantity){
int restStock = this.stockQuantity - quantity;
if(restStock<0){
throw new NotEnoughStockException("need more stock");
}
this.stockQuantity = restStock;
}
}
package jpabook.jpashop.domain.item;
import lombok.Getter;
import lombok.Setter;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
@Entity
@DiscriminatorValue("B")
@Getter @Setter
public class Book extends Item{
private String author;
private String isbn;
}
답변 3
0
0
Starting Gradle Daemon...
Gradle Daemon started in 2 s 904 ms
> Task :compileJava
> Task :processResources UP-TO-DATE
> Task :classes
> Task :compileTestJava FAILED
C:\������\Ȱ��1\jpashop\src\test\java\jpabook\jpashop\service\OrderServiceTest.java:47: error: cannot find symbol
Long orderId = orderService.order(member.getId(), book.getId(), orderCount);
^
symbol: method getId()
location: variable book of type Book
C:\������\Ȱ��1\jpashop\src\test\java\jpabook\jpashop\service\OrderServiceTest.java:55: error: cannot find symbol
assertEquals("�ֹ� ������ŭ ��� �پ�� �Ѵ�.", 8, book.getStockQuantity());
^
symbol: method getStockQuantity()
location: variable book of type Book
C:\������\Ȱ��1\jpashop\src\test\java\jpabook\jpashop\service\OrderServiceTest.java:68: error: cannot find symbol
Long orderId = orderService.order(member.getId(), item.getId(), orderCount);
^
symbol: method getId()
location: variable item of type Book
C:\������\Ȱ��1\jpashop\src\test\java\jpabook\jpashop\service\OrderServiceTest.java:75: error: cannot find symbol
assertEquals("�ֹ��� ��ҵ� ��ǰ�� ��ŭ ��� �����ؾ��Ѵ�.", 10, item.getStockQuantity());
^
symbol: method getStockQuantity()
location: variable item of type Book
C:\������\Ȱ��1\jpashop\src\test\java\jpabook\jpashop\service\OrderServiceTest.java:87: error: incompatible types: Book cannot be converted to Item
Item item = createBook("�ð� JPA", 10000, 10);
^
C:\������\Ȱ��1\jpashop\src\test\java\jpabook\jpashop\service\OrderServiceTest.java:101: error: cannot find symbol
book.setName(name);
^
symbol: method setName(String)
location: variable book of type Book
C:\������\Ȱ��1\jpashop\src\test\java\jpabook\jpashop\service\OrderServiceTest.java:102: error: cannot find symbol
book.setPrice(price);
^
symbol: method setPrice(int)
location: variable book of type Book
C:\������\Ȱ��1\jpashop\src\test\java\jpabook\jpashop\service\OrderServiceTest.java:103: error: cannot find symbol
book.setStockQuantity(stockQuantity);
^
symbol: method setStockQuantity(int)
location: variable book of type Book
8 errors
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileTestJava'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 22s
3 actionable tasks: 2 executed, 1 up-to-date
확인 감사합니다.
해당 에러만으로는 어떤 부분이 문제인지 파악하기 어렵네요.
아래 가이드를 참고하셔서 링크를 올려주시면 제가 직접 코드를 보면서 다시 확인해보겠습니다.
전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.
구글 드라이브 업로드 방법은 다음을 참고해주세요.
https://bit.ly/3fX6ygx
주의: 업로드시 권한 문제 꼭 확인해주세요