Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸš€ 3단계 - ν…ŒμŠ€νŠΈλ₯Ό ν†΅ν•œ μ½”λ“œ 보호 #825

Open
wants to merge 20 commits into
base: sumiini
Choose a base branch
from

Conversation

sumiini
Copy link

@sumiini sumiini commented Feb 3, 2025

μ•ˆλ…•ν•˜μ„Έμš”. πŸ˜€

μ„œλΉ„μŠ€λ³„λ‘œ 정상 μΌ€μ΄μŠ€, μ˜ˆμ™Έ 검증 μΌ€μ΄μŠ€ κ΅¬λΆ„ν•˜μ˜€μŠ΅λ‹ˆλ‹€.
ν•˜λ©΄μ„œ μ–΄λ €μ› λ˜ 뢀뢄은 ν…ŒμŠ€νŠΈ μ½”λ“œμ˜ ν•¨μˆ˜ λͺ…을 μ§“λŠ” κ²ƒμ΄μ—ˆμŠ΅λ‹ˆλ‹€.
이 λΆ€λΆ„μ—μ„œ λ§Žμ€ μ‹œκ°„μ„ ν• μ• ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

μ΄λ²ˆμ„ κ³„κΈ°λ‘œ SpringBootTest λ₯Ό 많이 μ—°μŠ΅ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

이후 μ‹œκ°„μ΄ λœλ‹€λ©΄, @ExtendWith(MockitoExtension.class) λ‹¨μœ„ ν…ŒμŠ€νŠΈλ„ μΆ”κ°€λ‘œ κ΅¬ν˜„ν•˜μ—¬
많이 μ—°μŠ΅ν•˜λ €ν•©λ‹ˆλ‹€ !

hongsumin added 19 commits January 21, 2025 21:59
Copy link

@sah3122 sah3122 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„± λ„ˆλ¬΄ μž˜ν•΄μ£Όμ…¨μŠ΅λ‹ˆλ‹€ πŸ‘
λͺ‡κ°€μ§€ 생각거리λ₯Ό λ‚¨κ²¨λ‘μ—ˆλŠ”λ° 확인 λΆ€νƒλ“œλ¦½λ‹ˆλ‹€.
질문이 μžˆλ‹€λ©΄ νŽΈν•˜κ²Œ DM μ£Όμ„Έμš” πŸ˜„

@Test
@Transactional
void create() {
//given
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given / when / then 을 μ£Όμ„μœΌλ‘œ ν‘œκΈ°ν•˜κΈ° 보단 곡백 라인을 톡해 μΆ©λΆ„νžˆ ν‘œν˜„ν• μˆ˜ μžˆμ„κ²ƒ κ°™μŠ΅λ‹ˆλ‹€ πŸ˜„


@DisplayName(value = "메뉴 κ·Έλ£Ή 생성 μ‹œ κ·Έλ£Ή λͺ…이 null일 경우 IllegalArgumentException μ˜ˆμ™Έ 처리λ₯Ό ν•œλ‹€.")
@Test
@Transactional
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@transactional μ–΄λ…Έν…Œμ΄μ…˜μ΄ μ–΄λ–€ 역할을 ν•˜κ³  μžˆμ„κΉŒμš” πŸ˜„

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λͺ¨λ‘ μ •μƒμ μœΌλ‘œ μ‹€ν–‰λ˜λ©΄ μ •μƒμ μœΌλ‘œ μ‹€ν–‰λ˜λ©΄ μ»€λ°‹λ˜κ³ , 쀑간에 μ˜ˆμ™Έκ°€ λ°œμƒν•˜λ©΄ λ‘€λ°±ν•΄μ£ΌλŠ” 역할을 ν•©λ‹ˆλ‹€.

μœ„λŠ” ν•œ createλΌλŠ” λ©”μ„œλ“œλ₯Ό νŠΈλžœμž­μ…˜μœΌλ‘œ λͺ…μ‹œν•œ κ²ƒμž…λ‹ˆλ‹€.
메뉴 κ·Έλ£Ή μƒμ„±μ‹œ μ˜ˆμ™Έκ°€ λ°œμƒν•˜λ©΄, 메뉴 그룹을 DB에 μ €μž₯ν•˜μ§€ μ•Šλ„λ‘ ν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SpringBootTest + @transactional μ–΄λ…Έν…Œμ΄μ…˜μ˜ 역할은 ν…ŒμŠ€νŠΈ μ½”λ“œκ°€ μ •μƒμ μœΌλ‘œ μ‹€ν–‰λ˜μ–΄ 컀밋이 λ˜μ—ˆμ„λ•Œ 둀백을 ν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€.
ν˜„μž¬ μž‘μ„±ν•΄μ£Όμ‹  ν…ŒμŠ€νŠΈ μ½”λ“œλŠ” μ‹€νŒ¨λ₯Ό κ²€μ¦ν•˜λŠ” ν…ŒμŠ€νŠΈ μ½”λ“œμ΄κΈ° λ•Œλ¬Έμ— λͺ…μ‹œν•˜μ§€ μ•Šμ•„λ„ 될것 κ°™μ•„μš” !

Comment on lines 34 to 36
String name = "ν•œλ§ˆλ¦¬λ©”λ‰΄";
MenuGroup request = new MenuGroup();
request.setName(name);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자주 μ‚¬μš©ν•˜λŠ” ν…ŒμŠ€νŠΈ 객체λ₯Ό TestFixture둜 μ •μ˜ν•˜μ—¬ 가독성과 μž¬μ‚¬μš©μ„±μ„ λ†’μΌμˆ˜ μžˆμŠ΅λ‹ˆλ‹€ πŸ˜„
이번 λ―Έμ…˜μ„ μ§„ν–‰ν•˜λ©° TestFixture에 λŒ€ν•΄ κ²½ν—˜ν•΄λ³΄λŠ”κ²ƒμ„ ꢌμž₯ λ“œλ €μš” !
https://velog.io/@langoustine/Test-Fixture

.isThrownBy(() -> menuGroupService.create(request));
}

@DisplayName(value = "메뉴 κ·Έλ£Ή 생성 μ‹œ κ·Έλ£Ή λͺ…이 빈 값일 경우 IllegalArgumentException μ˜ˆμ™Έ 처리λ₯Ό ν•œλ‹€.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Junit은 ν…ŒμŠ€νŠΈλ₯Ό μž‘μ„±ν•˜κΈ° μœ„ν•œ λ‹€μ–‘ν•œ κΈ°λŠ₯을 μ œκ³΅ν•©λ‹ˆλ‹€.
@NullAndEmptySource와 같은 ParameterizedTest에 λŒ€ν•΄ ν•™μŠ΅ν•΄λ³΄μ„Έμš” πŸ˜„
https://gmlwjd9405.github.io/2019/11/27/junit5-guide-parameterized-test.html

import java.util.NoSuchElementException;
import java.util.UUID;

@SpringBootTest
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SpringBootTestλ₯Ό ν™œμš©ν•œ 톡합 ν…ŒμŠ€νŠΈλ₯Ό μž‘μ„±ν•˜λŠ”κ²ƒλ„ μ’‹μ§€λ§Œ Mocking을 ν™œμš©ν•œ λ‹¨μœ„ ν…ŒμŠ€νŠΈ μž‘μ„±λ²•μ— λŒ€ν•΄μ„œλ„ κ²½ν—˜ν•΄λ³΄λŠ”κ²ƒμ„ ꢌμž₯ λ“œλ¦½λ‹ˆλ‹€ πŸ˜„


@DisplayName("메뉴 생성 μ‹œ 메뉴 가격이 null 이면 IllegalArgumentException μ˜ˆμ™Έ 처리λ₯Ό ν•œλ‹€.")
@Test
void canNotCreateMenuIfMenuPriceIsNull() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν…ŒμŠ€νŠΈ μ½”λ“œμ˜ν•¨μˆ˜λͺ…을 μ§“λŠ”κ²ƒμ— λŒ€ν•΄ 어렀움을 κ²ͺμœΌμ…¨λ‹€κ³  λ‚¨κ²¨μ£Όμ…¨λŠ”λ° 개인적으둜 ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν• λ•Œ 가독성을 μœ„ν•΄ ν•¨μˆ˜λͺ…을 ν•œκΈ€λ‘œ 짓기도 ν•©λ‹ˆλ‹€ πŸ˜„

Suggested change
void canNotCreateMenuIfMenuPriceIsNull() {
void `메뉴생성_μ‹€νŒ¨_가격_Null`() {

Assertions.assertThat(result).extracting(Menu::getId).containsExactly(menu.getId());

}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

파일의 λ§ˆμ§€λ§‰μ€ new line 으둜 λλ‚΄λŠ”κ²ƒμ„ ꢌμž₯ λ“œλ¦½λ‹ˆλ‹€ πŸ˜„
https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline
intellijλ₯Ό μ‚¬μš©ν•˜κ³  κ³„μ‹œλ‹€λ©΄ Setting -> Editor -> General οΏ½μ—μ„œΒ Ensure every saved file ends with a line break 을 μΌœμ£Όμ‹œλ©΄ λ©λ‹ˆλ‹€ πŸ˜„


@SpringBootTest
@Transactional
class OrderServiceTest {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κΌΌκΌΌν•œ ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„± πŸ‘
μ£Όλ¬Έ μœ ν˜•μ— 따라 λ‹¬λΌμ§€λŠ” ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€κ°€ μžˆμŠ΅λ‹ˆλ‹€.
@nested λ₯Ό ν™œμš©ν•˜μ—¬ μ£Όλ¬Έ μœ ν˜•μ— λ”°λ₯Έ μΌ€μŠ€νŠΈ μΌ€μ΄μŠ€λ₯Ό λΆ„λ¦¬ν•΄λ³΄λŠ”κ²ƒλ„ 쒋을것 κ°™μŠ΅λ‹ˆλ‹€ πŸ˜„
https://www.baeldung.com/junit-5-nested-test-classes

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ£Όλ¬Έκ³Ό κ΄€λ ¨λœ ν…ŒμŠ€νŠΈ μ½”λ“œκ°€ λΆ€μ‘±ν•œκ²ƒ κ°™μŠ΅λ‹ˆλ‹€.
배달 μ£Όλ¬Έ / 포μž₯ μ£Όλ¬Έ / 맀μž₯ 식사 μ£Όλ¬Έ κ³Ό 같이 μœ ν˜•λ³„λ‘œ μ •μ˜ν•΄μ£Όμ‹  μš”κ΅¬μ‚¬ν•­μ„ 기반으둜 μ‘°κΈˆλ” μƒμ„Έν•˜κ²Œ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ³΄μ„Έμš” πŸ˜„
μž‘μ„±ν•΄μ£Όμ‹  μš”κ΅¬μ‚¬ν•­ λ¬Έμ„œκ°€ λͺ¨λ‘ ν…ŒμŠ€νŠΈ μ½”λ“œμ— λ…Ήμ•„μ Έ μƒˆλ‘œμš΄ κ°œλ°œμžκ°€ ν”„λ‘œμ νŠΈμ— μ°Έμ—¬μ‹œ ν…ŒμŠ€νŠΈ μ½”λ“œλ§ŒμœΌλ‘œ μš”κ΅¬μ‚¬ν•­μ„ λΆ„μ„ν• μˆ˜ μžˆμ„ 정도가 되면 쒋을것 κ°™μ•„μš” πŸ˜„

@sumiini
Copy link
Author

sumiini commented Feb 10, 2025

리뷰 λ‚΄μš©μ„ λ°˜μ˜ν•˜μ—¬ 전체 ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μˆ˜μ •ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

@nested, Test Fixture, @ParameterizedTestλ₯Ό 적극 ν™œμš©ν•˜μ—¬
이전 μ½”λ“œλ³΄λ‹€ 가독성, μœ μ§€λ³΄μˆ˜μ„±, μž¬μ‚¬μš©μ„±μ΄ 크게 ν–₯상 λ˜λŠ” 것을 λŠκΌˆμŠ΅λ‹ˆλ‹€ πŸ™‚

Copy link

@sah3122 sah3122 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ν”Όλ“œλ°± 반영 μž˜ν•΄μ£Όμ…¨μŠ΅λ‹ˆλ‹€ πŸ‘
μ£Όλ¬Έ μ„œλΉ„μŠ€μ— λŒ€ν•œ ν…ŒμŠ€νŠΈ μ½”λ“œ μž‘μ„±μ΄ λΆ€μ‘±ν•œκ²ƒ κ°™μ•„ 보완 μš”μ²­ λ“œλ¦΄κ²Œμš” πŸ™
μΆ”ν›„ 진행될 λ―Έμ…˜μ—μ„œ 기쑴에 μž‘μ„±λœ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό ν™œμš©ν• μˆ˜ 있기 λ•Œλ¬Έμ— 쑰금만 더 νž˜λ‚΄μ£Όμ‹œκΈΈ λ°”λžλ‹ˆλ‹€ !


@SpringBootTest
@Transactional
class OrderServiceTest {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ£Όλ¬Έκ³Ό κ΄€λ ¨λœ ν…ŒμŠ€νŠΈ μ½”λ“œκ°€ λΆ€μ‘±ν•œκ²ƒ κ°™μŠ΅λ‹ˆλ‹€.
배달 μ£Όλ¬Έ / 포μž₯ μ£Όλ¬Έ / 맀μž₯ 식사 μ£Όλ¬Έ κ³Ό 같이 μœ ν˜•λ³„λ‘œ μ •μ˜ν•΄μ£Όμ‹  μš”κ΅¬μ‚¬ν•­μ„ 기반으둜 μ‘°κΈˆλ” μƒμ„Έν•˜κ²Œ ν…ŒμŠ€νŠΈ μ½”λ“œλ₯Ό μž‘μ„±ν•΄λ³΄μ„Έμš” πŸ˜„
μž‘μ„±ν•΄μ£Όμ‹  μš”κ΅¬μ‚¬ν•­ λ¬Έμ„œκ°€ λͺ¨λ‘ ν…ŒμŠ€νŠΈ μ½”λ“œμ— λ…Ήμ•„μ Έ μƒˆλ‘œμš΄ κ°œλ°œμžκ°€ ν”„λ‘œμ νŠΈμ— μ°Έμ—¬μ‹œ ν…ŒμŠ€νŠΈ μ½”λ“œλ§ŒμœΌλ‘œ μš”κ΅¬μ‚¬ν•­μ„ λΆ„μ„ν• μˆ˜ μžˆμ„ 정도가 되면 쒋을것 κ°™μ•„μš” πŸ˜„


@DisplayName(value = "메뉴 κ·Έλ£Ή 생성 μ‹œ κ·Έλ£Ή λͺ…이 null일 경우 IllegalArgumentException μ˜ˆμ™Έ 처리λ₯Ό ν•œλ‹€.")
@Test
@Transactional
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SpringBootTest + @transactional μ–΄λ…Έν…Œμ΄μ…˜μ˜ 역할은 ν…ŒμŠ€νŠΈ μ½”λ“œκ°€ μ •μƒμ μœΌλ‘œ μ‹€ν–‰λ˜μ–΄ 컀밋이 λ˜μ—ˆμ„λ•Œ 둀백을 ν•˜λŠ” 역할을 ν•©λ‹ˆλ‹€.
ν˜„μž¬ μž‘μ„±ν•΄μ£Όμ‹  ν…ŒμŠ€νŠΈ μ½”λ“œλŠ” μ‹€νŒ¨λ₯Ό κ²€μ¦ν•˜λŠ” ν…ŒμŠ€νŠΈ μ½”λ“œμ΄κΈ° λ•Œλ¬Έμ— λͺ…μ‹œν•˜μ§€ μ•Šμ•„λ„ 될것 κ°™μ•„μš” !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants