3์ฃผ์ฐจ
์นด์นด์ค ํ
ํฌ ์บ ํผ์ค 2๋จ๊ณ - BE - 3์ฃผ์ฐจ ํด๋ก ๊ณผ์
๊ณผ์ ๋ช
๊ณผ์ ์ค๋ช
1. ๋ ํฌ์งํ ๋ฆฌ ๋จ์ํ
์คํธ๋ฅผ ๊ตฌํํ์ฌ ์์ค์ฝ๋๋ฅผ ์ ์ถํ์์ค.
2. ์ฟผ๋ฆฌ๋ฅผ ํ
์คํธํ๋ฉด์ ๊ฐ์ฅ ์ข์ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด๋ณด์์ค.
๊ณผ์ ์์ธ : ์๊ฐ์๋ค์ด ๊ณผ์ ๋ฅผ ์งํํ ๋, ์ ๋ ํด์ผํ ๊ฒ
์๋ ํญ๋ชฉ์ ๋ฐ๋์ ํฌํจํ์ฌ ๊ณผ์ ์ํํด์ฃผ์ธ์!
๋ ํฌ์งํ ๋ฆฌ ๋จ์ํ ์คํธ๊ฐ ๊ตฌํ๋์๋๊ฐ?
ํ ์คํธ ๋ฉ์๋๋ผ๋ฆฌ ์ ๊ธฐ์ ์ผ๋ก ์ฐ๊ฒฐ๋์ง ์์๋๊ฐ? (ํ ์คํธ๋ ๊ฒฉ๋ฆฌ์ฑ์ด ํ์ํ๋ค)
Persistene Context๋ฅผ clearํ์ฌ์ ํ ์คํธ๊ฐ ๊ตฌํ๋์๋๊ฐ? (๋๋ฏธ๋ฐ์ดํฐ๋ฅผ JPA๋ฅผ ์ด์ฉํด์ insert ํ ์์ ์ธ๋ฐ, ๋ ํฌ์งํ ๋ฆฌ ํ ์คํธ์์ ์์ํ๋ ๋ฐ์ดํฐ ๋๋ฌธ์ ์ฟผ๋ฆฌ๋ฅผ ์ ๋๋ก ๋ณด์ง ๋ชปํ ์ ์๊ธฐ ๋๋ฌธ์)
ํ ์คํธ ์ฝ๋์ ์ฟผ๋ฆฌ ๊ด๋ จ๋ ๋ฉ์๋๊ฐ ๋๋ฌด ๋ง์ select๋ฅผ ์ ๋ฐํ์ง ์๋์ง? (์ ์ ํ ํ๋ฐฉ์ฟผ๋ฆฌ, ํจ์จ์ ์ธ in query, N+1 ๋ฌธ์ ๋ฑ์ด ํด๊ฒฐ๋ ์ฟผ๋ฆฌ)
BDD ํจํด์ผ๋ก ๊ตฌํ๋์๋๊ฐ? given, when, then
1. ๋ ํฌ์งํ ๋ฆฌ ๋จ์ํ ์คํธ
01) ๋ ํฌ์งํ ๋ฆฌ ๋จ์ํ ์คํธ๋ฅผ ๊ตฌํํ์ฌ ์์ค์ฝ๋๋ฅผ ์ ๋ก๋ํ์์ค.
๐ ์ฌ์ ๊ฐ๋
โ ฐ. ํ ์คํธ ๋ฐฉ๋ฒ๋ก
TDD(ํ ์คํธ ์ฃผ๋ ๊ฐ๋ฐ)์ ๊ฐ๋ฐ ์ ๊ทผ ๋ฐฉ์
- Outside In
- Inside Out
1. Outside In
- ํ ์คํธ ์ฃผ๋ ๊ฐ๋ฐ์ ์ผ๋ฐ์ ์ธ ์ ๊ทผ ๋ฐฉ์
- ์ธ๋ถ ๋์์ด๋ ์์คํ ์ ์ฌ์ฉ์ ์ธํฐํ์ด์ค(UI)๋ถํฐ ์์ํ์ฌ ๋ด๋ถ ๊ตฌํ์ผ๋ก ์งํ
- ์ฌ์ฉ์์ ์๊ตฌ์ฌํญ์ด๋ ๊ธฐ๋ฅ์ ๋จผ์ ์ ์ํ๊ณ ํ ์คํธ ์ผ์ด์ค๋ฅผ ์์ฑ
- ํ ์คํธ ์ผ์ด์ค ์์ฑ ํ์๋ ํด๋น ํ ์คํธ๋ฅผ ํต๊ณผํ ์ ์๋ ์ฝ๋๋ฅผ ์์ฑ
- ์ธ๋ถ์์ ๋ด๋ถ๋ก ์งํํ๋ฉด์ ํ์ํ ๊ฐ์ฒด ๋๋ ๋ชจ๋๋ค์ ์คํ ๋๋ ๋ชฉ(mock)์ผ๋ก ๋์ฒดํ์ฌ ํ ์คํธ๋ฅผ ์งํ
- ์ธ๋ถ์ ๋ด๋ถ ๊ฐ์ ํ๋ ฅ์ด ์ค์ํ ๊ฒฝ์ฐ์ ์ ์ฉํ ๋ฐฉ์
2. Inside Out
- ๋ด๋ถ ๊ตฌํ๋ถํฐ ์์ํ์ฌ ์ธ๋ถ ๋์์ด๋ UI๋ก ์งํ
- ์์คํ ์ ๋ด๋ถ์์ ์์ํ์ฌ ์ธ๋ถ๋ก ํฅํด๊ฐ๋ ๋ฐฉ์
- ์์คํ ์ ๋ด๋ถ ๋ก์ง์ด๋ ๊ตฌํ๋ถํฐ ์์ํ์ฌ ํ ์คํธ ์ผ์ด์ค๋ฅผ ์์ฑ
- ๋ด๋ถ ๊ตฌํ์ ์ด์ ์ ๋ง์ถ๊ณ ํด๋น ๊ตฌํ์ ํ ์คํธํ๊ธฐ ์ํด ํ์ํ ํ ์คํธ ์ผ์ด์ค๋ฅผ ์์ฑ
- ๋ด๋ถ ๊ตฌํ์ ์๋ฃํ๊ณ ํ ์คํธ๋ฅผ ํต๊ณผํ ํ์๋ ์ธ๋ถ ๋์์ด๋ UI๋ฅผ ๊ฐ๋ฐํ๋ ๋จ๊ณ๋ก ์งํ
- ๋ด๋ถ ๋ก์ง์ ์ ํ์ฑ์ ๋ ์ค์ ์ ๋
Outside In๊ณผ Inside Out์ ๊ฐ๋ฐ์๊ฐ ์์คํ ์ ๊ฐ๋ฐํ๊ณ ํ ์คํธํ๋ ๋ฐฉ์์ ๋ํ ๊ด์ ์์ ์ฐจ์ด๊ฐ ์๋ค. ๊ฐ ๋ฐฉ์์ ์ํฉ๊ณผ ์๊ตฌ์ฌํญ์ ๋ฐ๋ผ ์ ํฉํ ๊ฒฝ์ฐ๊ฐ ์์ผ๋ฉฐ, ํ๋ก์ ํธ์ ํน์ฑ์ ๋ฐ๋ผ ์ ํํ ์ ์๋ค.
โ ฑ. ์ฐ๊ด๊ด๊ณ
Hibernate๋ ๊ฐ์ฒด-๊ด๊ณ ๋งคํ(Object-Relational Mapping, ORM)์ ์ง์ํ์ฌ, ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋ ๋ฐ์์ ์ฌ์ฉ๋๋ ๊ฐ์ฒด์ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ ์ด๋ธ ๊ฐ์ ์ฐ๊ด๊ด๊ณ๋ฅผ ๋งคํํ ์ ์๋ค. Hibernate์์๋ ๋ค์๊ณผ ๊ฐ์ ์ฐ๊ด๊ด๊ณ๋ฅผ ์ง์ํ๋ค.
1. ์ผ๋์ผ(One-to-One) : ์ฐ๊ด๊ด๊ณ ๋ ๊ฐ์ ์ํฐํฐ ๊ฐ์ ํ๋์ ๊ด๊ณ๋ง ์กด์ฌํ๋ ๊ฒฝ์ฐ
2. ์ผ๋๋ค(One-to-Many) : ์ฐ๊ด๊ด๊ณ ํ๋์ ์ํฐํฐ๊ฐ ๋ค๋ฅธ ์ํฐํฐ ์ฌ๋ฌ ๊ฐ์ ์ฐ๊ด๋ ๊ฒฝ์ฐ
3. ๋ค๋์ผ(Many-to-One) : ์ฐ๊ด๊ด๊ณ ์ฌ๋ฌ ๊ฐ์ ์ํฐํฐ๊ฐ ํ๋์ ์ํฐํฐ์ ์ฐ๊ด๋ ๊ฒฝ์ฐ
4. ๋ค๋๋ค(Many-to-Many) : ์ฐ๊ด๊ด๊ณ ์ฌ๋ฌ ๊ฐ์ ์ํฐํฐ๊ฐ ๋ค๋ฅธ ์ฌ๋ฌ ๊ฐ์ ์ํฐํฐ์ ์ฐ๊ด๋ ๊ฒฝ์ฐ
์ฐ๊ด๊ด๊ณ๋ฅผ ๋งคํํ ๋๋ @ManyToOne, @OneToMany, @OneToOne, @ManyToMany ๋ฑ์ ์ด๋ ธํ ์ด์ ์ ์ฌ์ฉํ์ฌ ๋งคํํ๋ค. ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ mappedBy ์์ฑ์ผ๋ก ์ค์ ํ ์ ์๋ค. Hibernate์์๋ ์ฐ๊ด๊ด๊ณ์ ์ค์ ์ ํตํด ๊ฐ์ฒด ๊ฐ์ ๊ด๊ณ๋ฅผ ๋งคํํ๊ณ , ์ด๋ฅผ ๋ฐํ์ผ๋ก SQL ์ฟผ๋ฆฌ๋ฅผ ์์ฑํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ์์ฉํ๋ค. User, Board, Reply ํ ์ด๋ธ์ ์ค๊ณํ๋ฉด ์๋์ ๊ฐ์ ์ฐ๊ด๊ด๊ณ๊ฐ ์ค์ ๋๋ค.
FK๋ N์ชฝ์ ๋ถ์ด๋ฉด ๋๋ฉฐ, N ์ชฝ์ด ํญ์ ์ฐ๊ด๊ด๊ณ์ ์ฃผ์ธ์ด๋ค.
User(1) - Board(N)
User(1) - Reply(N)
Board(1) - Reply(N)
โ ฒ. DataJpaTest
1. ์๋ฒ ๋๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค: @DataJpaTest ๋ฅผ ์ฌ์ฉํ๋ฉด ํ ์คํธ๋ฅผ ์ํ ์๋ฒ ๋๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์ฌ๋ฆฐ๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก H2 ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์ฌ์ฉ๋๋ฉฐ, ๊ฐ๊ฐ์ ํ ์คํธ๋ง๋ค ๋ ๋ฆฝ์ ์ธ ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฐ ์์ฑ๋๋ค.
2. JPA ์ค์ : @DataJpaTest ๋ JPA์ ๊ด๋ จ๋ ์ค์ ์ ์๋์ผ๋ก ๊ตฌ์ฑํ๋ค. ์ด๋ฅผ ํตํด JPA ์ํฐํฐ์ ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ํ ์คํธ๋ฅผ ์คํํ ์ ์๋ค.
3. ์คํ๋ง ๋ฐ์ดํฐ JPA ๋ฆฌํฌ์งํ ๋ฆฌ ์ค์บ: @DataJpaTest๋ ์คํ๋ง ๋ฐ์ดํฐ JPA ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ์ค์บํ์ฌ ํ ์คํธํ ์ ์๋ ํ๊ฒฝ์ ์ ๊ณตํ๋ค. ๋ฐ๋ผ์ JPA ์ํฐํฐ์ ๋ฆฌํฌ์งํ ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๋ฅผ ํ ์คํธํ ์ ์๋ค.
4. ํธ๋์ญ์ ๊ด๋ฆฌ: @DataJpaTest ๋ ๊ฐ๊ฐ์ ํ ์คํธ ๋ฉ์๋๋ฅผ ์คํํ ๋ ํธ๋์ญ์ ์ ์๋์ผ๋ก ์์ํ๊ณ , ํ ์คํธ๊ฐ ์๋ฃ๋๋ฉด ๋กค๋ฐฑํ๋ค. ์ด๋ฅผ ํตํด ํ ์คํธ ๊ฐ์ ๋ฐ์ดํฐ ์ํ๋ฅผ ๊ฒฉ๋ฆฌ์์ผ ๊ฐ๊ฐ์ ํ ์คํธ๊ฐ ๋ ๋ฆฝ์ ์ผ๋ก ์คํ๋ ์ ์๋ค.
5. ๊ธฐํ ์ง์ ๊ธฐ๋ฅ: @DataJpaTest ๋ JPA ๊ด๋ จ ๊ธฐ๋ฅ์ ์ํด ์ถ๊ฐ์ ์ธ ๊ธฐ๋ฅ๋ค์ ์ ๊ณตํ๋ค. ์๋ฅผ ๋ค์ด, TestEntityManager ๋ฅผ ์ฌ์ฉํ์ฌ ์ํฐํฐ๋ฅผ ํ ์คํธ์ฉ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ฝ๊ฒ ์ ์ฅํ๊ณ ์กฐํํ ์ ์ํ๋ค.
โ ณ. BDD ํจํด
Given-When-Then
- ํ ์คํธ ์ผ์ด์ค๋ฅผ ๊ตฌ์ฑํ๋ ํจํด์ผ๋ก, ํ ์คํธ์ ์ํฉ, ๋์, ๊ฒฐ๊ณผ๋ฅผ ๋ช ํํ๊ฒ ์ ์ํ๊ณ ํํํ๋ ๋ฐฉ๋ฒ
- ํ ์คํธ์ ๊ฐ๋ ์ฑ๊ณผ ์ดํด๋๋ฅผ ๋์ด๋ฉฐ, ํ ์คํธ ์ผ์ด์ค๋ฅผ ๊ตฌ์ฑํ ๋ ๋์
1. Given (์ฃผ์ด์ง ์ํฉ): ํ ์คํธ๋ฅผ ์ํํ๊ธฐ ์ํด ํ์ํ ์ด๊ธฐ ์ํ๋ฅผ ์ค์ ํ๋ค. ํ ์คํธ์ ํ์ํ ๊ฐ์ฒด, ๋ฐ์ดํฐ ๋๋ ํ๊ฒฝ์ ์ค๋นํ๋ค.
2. When (๋์): ์ค์ ๋ก ํ ์คํธํ๋ ค๋ ๋์์ด ๋ฐ์ํ๋ค. ํน์ ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฑฐ๋ ์ด๋ค ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ๋ ๋ฑ์ ๋์์ ์คํํ๋ค.
3. Then (๊ฒฐ๊ณผ): ๊ธฐ๋ํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ช ์ํ๋ค. ์์๋๋ ๋์์ ์ถ๋ ฅ, ์ํ ๋ณํ ๋๋ ์์ธ ๋ฐ์ ๋ฑ์ ํ์ธํ๋ค. ํ ์คํธ ๊ฒฐ๊ณผ๋ฅผ ๊ฒ์ฆํ๋ ๋จ์ธ๋ฌธ(assertion)์ ์ฌ์ฉํ์ฌ ์ค์ ๊ฒฐ๊ณผ์ ๊ธฐ๋ํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋น๊ตํ๋ค.
JPA Repository๋ฅผ ์ ๋ชฐ๋ผ์ ์ด๋ฒ ๊ณผ์ ์ง์ง ๋ง์ด ๋ง๋งํ๋ค....
๊ทธ๋์ JPA Repository๋ถํฐ ๊ณต๋ถํจ
1. CartJPARepositoryTest
@Import(ObjectMapper.class)
@DataJpaTest
public class CartJPARepositoryTest extends DummyEntity {
@Autowired
private EntityManager em;
@Autowired
private ProductJPARepository productJPARepository;
@Autowired
private OptionJPARepository optionJPARepository;
@Autowired
private CartJPARepository cartJPARepository;
@Autowired
private UserJPARepository userJPARepository;
@Autowired
private ObjectMapper om;
@BeforeEach
public void setUp(){
// ๋๋ฏธ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
// user ์ ์ฅ
User user = userJPARepository.save(newUser("jihye"));
// product, option Dummy ๋ฐ์ดํฐ๋ฅผ ๋ฆฌ์คํธ๋ก ๋ถ๋ฌ์ด
List<Product> productListPS = productJPARepository.saveAll(productDummyList());
List<Option> optionListPS = optionJPARepository.saveAll(optionDummyList(productListPS));
// newCart(User user, Option option, Integer quantity)
cartJPARepository.save(newCart(user, optionListPS.get(0),1));
cartJPARepository.save(newCart(user, optionListPS.get(1),1));
em.clear();
}
}
@Autowired : ์คํ๋ง DI(Dependency Injection)์์ ์ฌ์ฉ๋๋ ์ด๋ ธํ ์ด์
์คํ๋ง์์ ๋น ์ธ์คํด์ค๊ฐ ์์ฑ๋ ์ดํ @Autowired๋ฅผ ์ค์ ํ ๋ฉ์๋๊ฐ ์๋์ผ๋ก ํธ์ถ๋๊ณ , ์ธ์คํด์ค๊ฐ ์๋์ผ๋ก ์ฃผ์ ๋๋ค. ์ฆ, ํด๋น ๋ณ์ ๋ฐ ๋ฉ์๋์ ์คํ๋ง์ด ๊ด๋ฆฌํ๋ Bean์ ์๋์ผ๋ก ๋งคํํด์ฃผ๋ ๊ฐ๋ ์ด๋ค. @Autowired ๋ ๋ณ์, Setter๋ฉ์๋, ์์ฑ์, ์ผ๋ฐ ๋ฉ์๋์ ์ ์ฉ์ด ๊ฐ๋ฅํ๋ค.
EntityManager๋ JPA Repository์์ ์ ๊ณตํ๋ ๊ฒ์ผ๋ก, ์ํฐํฐ๋ฅผ ๊ด๋ฆฌํ๋ ์ญํ ์ ์ํํ๋ค.
๋จผ์ , setUp() ๋ฉ์๋๋ฅผ ํตํด ๋๋ฏธ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์จ๋ค. ์ํ ๋ฆฌ์คํธ์ ์ต์ ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค์ด ๋๋ฏธ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ cartJPARepository์ save() ๋ฉ์๋๋ฅผ ํตํด user์ ์ต์ , ์๋์ ์ ์ฅํ๋ค.
Cart Find Test
@DisplayName("์ฅ๋ฐ๊ตฌ๋ ์กฐํ ํ
์คํธ")
@Test
public void cart_findAll_test() {
//given
int id = 1;
//when
List<Cart> cartListPS = cartJPARepository.findByUserId(id);
//then
Assertions.assertThat(cartListPS.get(0).getQuantity()).isEqualTo(1);
Assertions.assertThat(cartListPS.get(0).getPrice()).isEqualTo(10000);
Assertions.assertThat(cartListPS.get(0).getOption().getOptionName()).isEqualTo("01. ์ฌ๋ผ์ด๋ฉ ์งํผ๋ฐฑ ํฌ๋ฆฌ์ค๋ง์ค์๋์
4์ข
");
Assertions.assertThat(cartListPS.get(0).getUser().getUsername()).isEqualTo("jihye");
}
cart_findAll_test๋ ์ฅ๋ฐ๊ตฌ๋๋ฅผ ์กฐํํ๋ ํ ์คํธ์ด๋ค.
id๋ฅผ 1๋ก ์ฃผ๊ณ cartList ๋ด์์ ํด๋น ์์ด๋๋ฅผ ๊ฐ๋ cart๋ฅผ ์ฐพ๋๋ค.
๊ทธ๋ฐ ๋ค์ ์๋๊ณผ ๊ฐ๊ฒฉ, ์ต์ , ์ ์ ์ด๋ฆ ๋ฑ์ ํ์ธํ๋ค.
Cart Save Test
@DisplayName("์ฅ๋ฐ๊ตฌ๋ ๋ด๊ธฐ ํ
์คํธ")
@Test
public void cart_save_test() {
//given
User user = userJPARepository.findById(1).get();
Option option = optionJPARepository.findById(5).get();
newCart(user, option, 1);
//when
Cart cart = cartJPARepository.save(newCart(user, option, 1));
System.out.println("id : " + cart.getId());
//then
}
๋ค์์ผ๋ก ์ฅ๋ฐ๊ตฌ๋ ๋ด๊ธฐ๋ฅผ ํ ์คํธ ํ๋ค. newCart() ๋ฉ์๋๋ก ์ฅ๋ฐ๊ตฌ๋์ ๋ฃ๋๋ค.
Cart Update Test
@DisplayName("์ฅ๋ฐ๊ตฌ๋ ์
๋ฐ์ดํธ ํ
์คํธ")
@Test
public void cart_update_test(){
//given
int id = 1;
int quantity = 3;
int price;
Cart selectCart = cartJPARepository.findById(id).get();
//when
price = selectCart.getOption().getPrice() * quantity;
selectCart.update(quantity, price);
em.flush();
//then
}
selectCart๋ฅผ ํตํด ์ ํํ ์ฅ๋ฐ๊ตฌ๋๋ฅผ ์ ๋ฐ์ดํธ ํ๋ค.
์ ๋ฐ์ดํธ ํ์ price๋ ์ฅ๋ฐ๊ตฌ๋ ๋ด์ ์ต์ ์ ๊ฐ๊ฒฉ๊ณผ ์๋์ ๊ณฑํ์ฌ ๊ณ์ฐ ํ๋ค.
Cart Delete Test
@DisplayName("์ฅ๋ฐ๊ตฌ๋ ์ญ์ ํ
์คํธ")
@Test
public void cart_deleteByUserId_test(){
//given
int id = 1;
//when
cartJPARepository.deleteByUserId(id);
List<Cart> cartListPS = cartJPARepository.findByUserId(id);
//given
Assertions.assertThat(cartListPS.size()).isEqualTo(0);
}
2. OrderJPARepositoryTest
@Import(ObjectMapper.class)
@DataJpaTest
public class OrderJPARepositoryTest extends DummyEntity {
@Autowired
private EntityManager em;
@Autowired
private OrderJPARepository orderJPARepository;
@Autowired
private ItemJPARepository itemJPARepository;
@Autowired
private UserJPARepository userJPARepository;
@Autowired
private ProductJPARepository productJPARepository;
@Autowired
private OptionJPARepository optionJPARepository;
@Autowired
private CartJPARepository cartJPARepository;
@BeforeEach
public void setUp(){
User userPS = userJPARepository.save(newUser("jihye"));
List<Product> productListPS = productJPARepository.saveAll(productDummyList());
List<Option> optionListPS = optionJPARepository.saveAll(optionDummyList(productListPS));
Order orderPS = orderJPARepository.save(newOrder(userPS));
Cart cartPS1 = newCart(userPS, optionListPS.get(0), 1);
Cart cartPS2 = newCart(userPS, optionListPS.get(1), 1);
itemJPARepository.save(newItem(cartPS1, orderPS));
itemJPARepository.save(newItem(cartPS2, orderPS));
em.clear();
}
@DisplayName("์ฃผ๋ฌธ ํ
์คํธ")
@Test
public void item_findByOrderId_test(){
//given
int id = 1;
//when
List<Item> itemListPS = itemJPARepository.findByOrderId(id);
//then
Assertions.assertThat(itemListPS.get(0).getId()).isEqualTo(1);
Assertions.assertThat(itemListPS.get(0).getPrice()).isEqualTo(10000);
Assertions.assertThat(itemListPS.get(0).getQuantity()).isEqualTo(1);
Assertions.assertThat(itemListPS.get(0).getOrder().getId()).isEqualTo(1);
}
}
itemJPARepository ์ ์๋ก์ด ์ฃผ๋ฌธ ์์ดํ ๋ค์ ๋ฃ์ด๋๊ณ ์ฃผ๋ฌธ ๋ด์ญ์ด ๋ฐ์ดํฐ์ ๋ง๋์ง ๋น๊ตํ๋ค.
'๐ช kakaotech campus' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์นดํ ์ผ | [STEP2 cloncoding] 4์ฃผ์ฐจ ๊ณผ์ - (2) (0) | 2023.07.20 |
---|---|
์นดํ ์ผ | [STEP2 cloncoding] 4์ฃผ์ฐจ ๊ณผ์ - (1) (0) | 2023.07.19 |
์นดํ ์ผ | [STEP2 clone coding] 2์ฃผ์ฐจ ๊ณผ์ - (2) (0) | 2023.07.07 |
์นดํ ์ผ | [STEP2 clone coding] 2์ฃผ์ฐจ ๊ณผ์ - (1) (0) | 2023.07.04 |
์นดํ ์ผ | [STEP2 clone coding] 1์ฃผ์ฐจ ๊ณผ์ - (3) (0) | 2023.06.30 |