-
Notifications
You must be signed in to change notification settings - Fork 85
[이동훈] 1차시 미션 제출합니다. #106
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
base: dh2906
Are you sure you want to change the base?
[이동훈] 1차시 미션 제출합니다. #106
Conversation
1차시 수고하셨습니다! 궁금증에 대해서 저의 의견도 남겨보겠습니다!
|
public static final String EMPTY_STRING = "문자열이 비어있습니다."; | ||
public static final String CUSTOM_DELIMITER_NOT_FOUND = "커스텀 구분자를 찾을 수 없습니다."; | ||
public static final String NEGATIVE_NUMBER_NOT_ALLOWED = "음수는 처리할 수 없습니다."; | ||
public static final String INVALID_STRING = "문자열은 처리할 수 없습니다."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오! 이런식으로 에러메세지를 관리하니 아주 좋네요. 한수 배워갑니다 ㅎㅎ
src/main/java/domain/Calculator.java
Outdated
if (num2 == 0) | ||
throw new ArithmeticException(DIVIDE_BY_ZERO); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
public int calculate(String str) { | ||
if (str == null || str.isBlank()) { | ||
throw new RuntimeException(EMPTY_STRING); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
계산기 구현 요구사항에는
예: “” => 0
이런 식으로 되어있는데 이 로직이면 요구사항과 맞지 않아 보입니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 이건 제가 놓친 사항이네요!
알려주셔서 감사합니다!!
@ValueSource(strings = { | ||
"1,2,3:4", | ||
"1 , 2: 3,4 " | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이런 어노테이션도 있었군요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
어노테이션을 통해 테스트 메소드 파라미터에 값을 주입해줄 수 있습니다!
위와 비슷한 어노테이션이 많으니 찾아보시면 도움 되실 것 같아요!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1주차 미션 고생하셨습니다
남겨주신 궁금증에 대해 제 생각을 남길게요 😄
- 예외 케이스를 각각의 테스트 메서드로 분리하는 것이 맞나요?
- 저는 각각의 테스트 메서드로 분리하는 것이 맞다고 생각합니다. 예외 케이스별로 다 다른 상황이라고 생각하면
단위 테스트
의 역할에 부합한다고 생각합니다.
- 베스트 케이스에 대해서 값을 주입할 때는 보통 여러 형태의 값을 넘겨주는 편인가요?
- 여러 형태의 값을 넘겨주되 저는 보통 최대 3개까지 넘겨주는것을 선호합니다. 베스트 케이스인 만큼 3개를 초과하는 케이스의 경우 되려 코드가 지저분해질 수 있다고 생각해서 3개까지만 넣는 편입니다. 다만 이때 경계값을 위주로 테스트하고 있습니다.
- 테스트 코드를 작성해보니 대부분 코드가 비슷하거나 중복도 꽤 존재하는데, 비즈니스 로직과 다르게 테스트 코드는 이러한 부분은 어느정도 감수하고 넘어가도 될까요?
- 테스트 코드에서는 어느정도의 중복은 허용해도 된다고 생각합니다. 중복이 발생하는 부분은 보통 비슷한 로직에서 발생할것 같은데 비슷한 로직인만큼 중복 코드가 어느정도는 발생할 수밖에 없다고 생각합니다.
다른 분들의 의견과는 다를 수 있는 저의 의견이라 생각해서 동훈님만의 기준을 잡아보는것도 좋을것 같습니당
src/main/java/domain/Calculator.java
Outdated
|
||
public class Calculator { | ||
|
||
public static final String DIVIDE_BY_ZERO = "0으로 나눌 수 없습니다."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 에러 메시지를
static final
로 선언하면 어떤 장점이 있을까요? private
으로 안하신 이유가 있나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- static 선언으로 인해 객체의 생성 없이 사용 가능해지며, 외부 클래스인 테스트 클래스에서도 사용 가능합니다. final 선언을 통해 값의 불변성을 보장하여 런타임 중 변경될 수 없음을 나타낼 수 있습니다.
- 위에서 말씀드린대로 테스트 클래스에서도 사용가능 해야 하므로 public으로 선언했습니다. 그러나 해당 부분은 원래 enum을 통해 관리 하려고 했으나 ErrorMessage.DIVIDE_BY_ZERO.getMessage() 와 같이 코드가 길어져서 enum으로 관리하는 것은 잠시 포기했습니다. enum을 사용하는게 맞는 방식인거 같아 고민 해보고 리팩토링 진행해 보겠습니다!
public static final String DIVIDE_BY_ZERO = "0으로 나눌 수 없습니다."; | ||
|
||
public int add(int num1, int num2) { | ||
return Math.addExact(num1, num2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
~~Exact()
메서드는 어떤 장점이 있나요?
일반 연산자(+
, -
등) 과 어떤 차이점이 있나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
대표적인 예로 addExact는 비트 연산을 통해 int 타입인 두 수의 합이 int형의 저장 가능 범위를 벗어난다면 예외를 발생시켜주고 있습니다.
일반적인 연산자를 사용한다면 오버플로우 발생 시 부호 비트가 값의 크기를 나타내기 위한 비트로 사용되서 결과 값이 예상치 못한 값으로 나올 수 있습니다.
.toList(); | ||
} | ||
|
||
public int parseTokenToInt(String token) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
내부에서만 사용하는 메서드같아서 private
으로 선언해도 될 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아! 이걸 놓치고 있었네요!
수정해두겠습니다
src/test/java/CalculatorTest.java
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
다양한 상황에 대한 예외 테스트까지 잘 작성해주셨네요 👍
|
||
public class StringCalculatorTest { | ||
|
||
private final StringCalculator stringCalculator = new StringCalculator(); | ||
|
||
@ParameterizedTest |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이건 어떤 역할을 하는건가요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ParameterizedTest는 값을 외부에서 주입받는 테스트 임을 명시합니다.
@valuesource, @nullsource 등 다양한 어노테이션을 통해 메소드 파라미터에 값을 주입할 수 있으며 이를 통해 하나의 테스트 메소드에 여러 형태의 값에 대한 반복적인 테스트할 수 있습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 문자열 계산기를 주입하는 부분을 말씀하신거라면 문자열 계산기의 테스트를 위해 필드에서 객체를 생성했습니다.
혹은 @beforeach 를 통해 매번 테스트 메소드를 시작하기 전 초기화 작업을해줄 수도 있는데 여기서 주입을 하는 경우도 있습니다.
@DisplayName("공백을 줬을 경우 테스트") | ||
public void testEmptyValue(String value) { | ||
assertThat(StringCalculator.calculate(value)).isEqualTo(0); | ||
@CsvSource({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
신기한 어노테이션이 많네요! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
파싱에 대한 검증도 하시고 꼼꼼한 테스트 좋아요 👍
좋은 의견 감사합니다!! 덕분에 고민되는 부분에 대한 방향성을 잡을 수 있을 것 같아요! |
겨울 방학때 초록 스터디를 진행하며 머지된게 있다보니 문제 해결하고 겸사겸사 깃 공부하는데 시간이 꽤 걸린거 같네요..
깃 열심히 공부해야겠다는 생각이 듭니다..
아래는 미션을 진행하면서 생긴 궁금증을 질문으로 남겼습니다!
단위 테스트는 같은 기능에 대해서도 다양한 케이스를 각 메소드로 분할하여 작성하는 것이 맞나요?
음수인 케이스와 문자열인 케이스를 각각의 메소드로 분리하는 것이 맞는지 궁금합니다.
베스트 케이스에 대해서 값을 주입할 때는 보통 여러 형태의 값을 넘겨주는 편인가요?
테스트 코드를 작성해보니 대부분 코드가 비슷하거나 중복도 꽤 존재하는데, 비즈니스 로직과 다르게 테스트 코드는 이러한
부분은 어느정도 감수하고 넘어가도 될까요?