-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(follow): primitive long을 걷어낸다 (#11)
* test: long의 기본값은 0이 될 수 있으므로 위험하다 * refactor(follow): primitive long을 걷어낸다
- Loading branch information
Showing
3 changed files
with
51 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/test/java/com/demo/feedsystemdesign/study/PrimitiveTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.demo.feedsystemdesign.study; | ||
|
||
import org.junit.jupiter.api.DisplayNameGeneration; | ||
import org.junit.jupiter.api.DisplayNameGenerator; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class) | ||
public class PrimitiveTypeTest { | ||
|
||
@Test | ||
void long은_지역_내에서_값을_할당하지_않으면_컴파일_에러가_발생한다() { | ||
long a; | ||
|
||
// System.out.println("a = " + a); // 에러 발생 | ||
} | ||
|
||
@Test | ||
void 그러나_객체의_필드로_long을_사용하면_0으로_초기화된다() { | ||
class PrimitiveLongClass { | ||
long a; | ||
} | ||
|
||
PrimitiveLongClass testInstance = new PrimitiveLongClass(); | ||
|
||
assertThat(testInstance.a).isZero(); | ||
// 0으로 초기화되는 것은 의도와 다르다. | ||
// 더 정확한 제어를 위해서는 null이 들어가서 오류가 발생하는 편이 낫다. | ||
} | ||
|
||
@Test | ||
void 따라서_0을_기본값으로_사용하고_싶은_것이_아니라면_Long이_더_안전할수도_있다() { | ||
class ReferenceLongClass { | ||
Long a; | ||
} | ||
|
||
ReferenceLongClass testInstance = new ReferenceLongClass(); | ||
|
||
assertThat(testInstance.a).isNull(); | ||
} | ||
} |