Skip to content

Commit 563d83e

Browse files
committed
feat: Ladder 생성 유효성 검사 추가
1 parent 917f659 commit 563d83e

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

src/main/java/ladder/domain/Ladder.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package ladder.domain;
22

3+
import java.util.Objects;
4+
35
public class Ladder {
46

57
private final Participants participants;
68
private final LadderHeight height;
79
private final Lines lines;
810

911
public Ladder(final Participants participants, final LadderHeight height, final Lines lines) {
12+
validateOrThrow(participants, height, lines);
13+
1014
this.participants = participants;
1115
this.height = height;
1216
this.lines = lines;
@@ -28,4 +32,10 @@ public Line getLine(final int index) {
2832
return this.lines.getLines()
2933
.get(index);
3034
}
35+
36+
private void validateOrThrow(final Participants participants, final LadderHeight height, final Lines lines) {
37+
if (Objects.isNull(participants) || Objects.isNull(height) || Objects.isNull(lines)) {
38+
throw new IllegalArgumentException("null인 멤버 변수를 포함할 수 없습니다.");
39+
}
40+
}
3141
}

src/test/java/ladder/domain/LadderHeightTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.jupiter.api.DisplayName;
44
import org.junit.jupiter.api.Test;
55

6+
import static org.assertj.core.api.Assertions.assertThat;
67
import static org.assertj.core.api.Assertions.assertThatThrownBy;
78

89
public class LadderHeightTest {
@@ -14,4 +15,11 @@ void invalid() {
1415
.isInstanceOf(IllegalArgumentException.class)
1516
.hasMessageContaining("유효하지 않은 사다리 높이입니다.");
1617
}
18+
19+
@DisplayName("생성")
20+
@Test
21+
void create() {
22+
LadderHeight ladderHeight = new LadderHeight(7);
23+
assertThat(ladderHeight).isEqualTo(new LadderHeight(7));
24+
}
1725
}

src/test/java/ladder/domain/LadderTest.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import java.util.stream.Stream;
99

1010
import static org.assertj.core.api.Assertions.assertThat;
11+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
12+
import static org.junit.jupiter.api.Assertions.assertAll;
1113

1214
public class LadderTest {
1315

@@ -20,13 +22,30 @@ void create() {
2022
final LadderHeight ladderHeight = new LadderHeight(5);
2123
final Ladder ladder = new Ladder(participants, ladderHeight, new Lines(List.of(new Line(List.of(true)))));
2224

23-
assertThat(ladder.getParticipants()).isEqualTo(new Participants(Stream.of("woody", "jacob", "tate", "test")
24-
.map(Participant::new)
25-
.collect(Collectors.toList())));
26-
assertThat(ladder.getParticipants()
27-
.size()).isEqualTo(4);
28-
assertThat(ladder.getHeight()).isEqualTo(new LadderHeight(5));
29-
assertThat(ladder.getLines()
30-
.size()).isEqualTo(1);
25+
assertAll(
26+
() -> assertThat(ladder.getParticipants()
27+
.size()).isEqualTo(4),
28+
() -> assertThat(ladder.getHeight()).isEqualTo(new LadderHeight(5)),
29+
() -> assertThat(ladder.getLines()
30+
.size()).isEqualTo(1)
31+
);
32+
}
33+
34+
@DisplayName("사다리 생성 실패")
35+
@Test
36+
void invalid() {
37+
final Participants participants = new Participants(Stream.of("woody", "jacob", "tate", "test")
38+
.map(Participant::new)
39+
.collect(Collectors.toList()));
40+
final LadderHeight ladderHeight = new LadderHeight(5);
41+
final Ladder ladder = new Ladder(participants, ladderHeight, new Lines(List.of(new Line(List.of(true)))));
42+
43+
assertAll(
44+
() -> assertThatThrownBy(() -> new Ladder(null, ladderHeight,
45+
new Lines(List.of(new Line(List.of(true)))))),
46+
() -> assertThatThrownBy(() -> new Ladder(participants, null,
47+
new Lines(List.of(new Line(List.of(true)))))),
48+
() -> assertThatThrownBy(() -> new Ladder(participants, ladderHeight, null))
49+
);
3150
}
3251
}

src/test/java/ladder/domain/ParticipantsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.junit.jupiter.api.Test;
55

66
import java.util.Collections;
7+
import java.util.List;
78

9+
import static org.assertj.core.api.Assertions.assertThat;
810
import static org.assertj.core.api.Assertions.assertThatThrownBy;
911

1012
public class ParticipantsTest {
@@ -16,4 +18,11 @@ void invalid() {
1618
.isInstanceOf(IllegalArgumentException.class)
1719
.hasMessageContaining("참가자는 최소 1명 이상이여야 합니다.");
1820
}
21+
22+
@DisplayName("생성")
23+
@Test
24+
void create() {
25+
Participants participants = new Participants(List.of(new Participant("woody")));
26+
assertThat(participants).isEqualTo(new Participants(List.of(new Participant("woody"))));
27+
}
1928
}

0 commit comments

Comments
 (0)