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

Step 3 - 테스트를 통한 코드 보호 #840

Open
wants to merge 15 commits into
base: min-queue
Choose a base branch
from

Conversation

min-queue
Copy link

안녕하세요 리뷰어님 !
어제 강의 듣고 코드 업데이트 하다보니 조금 늦었습니다.
고민 진짜 많이하고 올렸는데 생각보다 양이 많네요..

제가 아직 테스트코드 작성하는게 익숙하지 않은지 조금 더디네요 ..

파이썬하고 TS로 백엔드 하다가, 이제 스프링 진영에 몸담은지 1년 조금 넘었는데
잘 맞게 작성하고 있는지 잘모르겠네요 ㅎㅎ;

감사합니다! 잘 부탁 드립니다.

@min-queue min-queue changed the title Step 3 Step 3 - 테스트 코드를 통한 보호 Feb 5, 2025
@min-queue min-queue changed the title Step 3 - 테스트 코드를 통한 보호 Step 3 - 테스트를 통한 코드 보호 Feb 5, 2025
Copy link

@kkt219a kkt219a left a comment

Choose a reason for hiding this comment

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

안녕하세요 민규님!

고민 진짜 많이하고 올렸는데 생각보다 양이 많네요..

고생 많으셨습니다 ㅠㅠ 열심히 코드 작성해주신 게 느껴져요 💯💯

제가 아직 테스트코드 작성하는게 익숙하지 않은지 조금 더디네요 ..
파이썬하고 TS로 백엔드 하다가, 이제 스프링 진영에 몸담은지 1년 조금 넘었는데
잘 맞게 작성하고 있는지 잘모르겠네요 ㅎㅎ;

충분히 잘 작성해주셨습니다! ㅎㅎ 리뷰하면서 코멘트를 남겼는데 확인 부탁드릴게요!
궁금하신 점 있으시면 편하게 댓글이나 DM으로 연락주세요🔥

@@ -0,0 +1,4 @@
package kitchenpos.application.Exception;

public class DeliveryAddressNotFoundException extends IllegalArgumentException {
Copy link

@kkt219a kkt219a Feb 5, 2025

Choose a reason for hiding this comment

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

예외를 종류별로 세분화 해보셨군요 💯
만드시는 데 고생하셨을 것 같아요🥲
민규님이 예외에 대한 의도가 있다면 충분한데,
유사한 예외들을 추상화한 상위 객체가 있거나, 어떨까하는 생각이 들었어요!
만약 주문 생성 시 발생할 수 있는 예외들을 공통 처리해주어야 한다면 예외를 캐치할 때
가능한 예외들을 모두 나열하거나, RuntimeException, Exception으로 포괄하여 표현할 수 밖에 없을 것 같아요! 이 부분은 어떻게 생각하실까요? 이 부분은 코멘트 주시면 추가 답변 하겠습니다!

Copy link
Author

Choose a reason for hiding this comment

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

아 규태님 감사합니다.
일단은 수업 들었을때 Exception을 쪼개는거까진 봤는데 그이후에 어떻게 작성하는게 좋을지 고민되었던 부분입니다.
혹시 제가 조금 참고할만한게 있을까요 ..?

Copy link
Author

Choose a reason for hiding this comment

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

// 1. 도메인 예외의 기본 추상 클래스
public abstract class OrderException extends RuntimeException {
    public OrderException(String message) {
        super(message);
    }
}

// 2. 특정 상황별 구체적인 예외들
public class OrderNotFoundException extends OrderException {
    public OrderNotFoundException(OrderId orderId) {
        super("Order not found with id: " + orderId);
    }
}
}

혹시 약간 이런식의 형태를 의미하는걸까요 ..?


import java.util.Set;

public class FakePurogmalumClient implements PurgomalumClient {
Copy link

Choose a reason for hiding this comment

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

Fake를 활용해 보셨군요 👍🏻👍🏻
혹시 이전에 Mock을 활용하신 경험이 있을까요?

Copy link
Author

Choose a reason for hiding this comment

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

아 기존에는 조금 써보긴 했는데, 아무래도 행위검증만 하는게 Mock이다 보니 테스트가 좀 복잡하면 잘깨지고 느리기도 했고,
지금은 코드 짜는데 급급하다 보니 테스트코드를 작성하질 않아서, 굉장히 오랜만(?)에 작성해보았습니다.

@@ -0,0 +1,29 @@
package kitchenpos.fake.repository;
Copy link

Choose a reason for hiding this comment

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

Fake 객체들에 대한 패키지 분리도 잘 해주셨군요 💯💯

Comment on lines +45 to +47
@Nested
@DisplayName("메뉴 등록")
class CreateMenu {
Copy link

Choose a reason for hiding this comment

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

@Nested를 잘 활용해보셨군요! 정책과 기능이 묶여서 가독성이 좋은 것 같아요 😊

Comment on lines 58 to 65
@DisplayName("이름이 없는 메뉴 그룹은 생성할 수 없다")
@NullAndEmptySource
@ParameterizedTest
void cannotMenuGroupWithoutName(String name) {
MenuGroup request = MenuFixture.menuGroup(name);

assertThatThrownBy(() -> menuGroupService.create(request))
.isInstanceOf(IllegalArgumentException.class);
Copy link

Choose a reason for hiding this comment

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

메뉴 그룹 생성은 @Nested로 안묶으신 의도 있으실까요?!

Copy link
Author

Choose a reason for hiding this comment

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

엇.. 놓쳤네요 호다닥 묶어놓겠습니다..

import java.util.List;
import java.util.UUID;

public class MenuFixture {
Copy link

Choose a reason for hiding this comment

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

Fixture 활용 👍🏻👍🏻

Comment on lines 47 to 49
assertThat(created.getId()).isNotNull();
assertThat(created.getName()).isEqualTo("후라이드");
assertThat(created.getPrice()).isEqualByComparingTo("16000");
Copy link

Choose a reason for hiding this comment

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

이 부분도 assertAll로 묶어주시면 좋을 것 같은데요!
혹시 민규님만의 기준이 있으실까요?!

Copy link
Author

Choose a reason for hiding this comment

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

급하게 작성하다보니 놓친게 좀 많네요 수정하도록하겠숩니다 ㅎㅎ

Product product = ProductFixture.product("후라이드", 16000);
productRepository.save(product);
Product request = new Product();
request.setPrice(BigDecimal.valueOf(18000));
Copy link

Choose a reason for hiding this comment

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

setter 사용은 지양하는게 좋다고 생각합니다!
만약 테스트할 객체에 setter가 없다면 테스트 코드를 위해 setter를 만들어줘야할까요?
ReflectionTestUtils를 활용해보면 좋을 것 같아요!
setter를 사용하는 테스트코드들 전체를 확인해보시면 좋을 것 같습니다!

Copy link
Author

Choose a reason for hiding this comment

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

Setter를 쓰는 부분이 사실 마음에 안들었는데 그방법이 있었네요..
감사합니다..

Comment on lines +21 to +35
public static Order deliveryOrder(String address, List<OrderLineItem> orderLineItems) {
Order order = order(OrderType.DELIVERY, OrderStatus.WAITING, orderLineItems);
order.setDeliveryAddress(address);
return order;
}

public static Order eatInOrder(UUID orderTableId, List<OrderLineItem> orderLineItems) {
Order order = order(OrderType.EAT_IN, OrderStatus.WAITING, orderLineItems);
order.setOrderTableId(orderTableId);
return order;
}

public static Order takeoutOrder(List<OrderLineItem> orderLineItems) {
return order(OrderType.TAKEOUT, OrderStatus.WAITING, orderLineItems);
}
Copy link

Choose a reason for hiding this comment

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

주문의 특정 상태를 픽스쳐로 잘 활용해주셨군요 👍🏻
이렇게 사용할 경우 단점은 어떤 게 있을까요? 답변 해주시면 감사하겠습니다 😃

Copy link
Author

Choose a reason for hiding this comment

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

이게 맞는지는 모르겠지만 새로운 형태의 Order들이 생길때마다 계속 testFixture를 만들어줘야되는게.. 뭔가
좀 비효율적이다 ? 라고 느끼고 있긴했는데 질문의 의도가 이게 맞으실지 틀리다면 어떤 것인지 궁금합니다 :)

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.

3 participants