Skip to content

Commit

Permalink
CollgeTest.java, MajorTest.java, MajorInfoServiceTest.java 테스트 코드 리팩토…
Browse files Browse the repository at this point in the history
…링, 메서드 이름 개선, 가독성 향상
  • Loading branch information
DongilMin committed Nov 3, 2024
1 parent 3b3211d commit d06d5b9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class CollegeTest {

@DisplayName("College 타입에 제목을 저장한다.")
@Test
public void College_is_OK() {
public void saveValidCollegeName() {

// given
String collegeString = "사회과학대학";
Expand All @@ -26,7 +26,7 @@ public void College_is_OK() {

@DisplayName("College 타입에 너무 긴 이름을 저장한다. 20자 이상")
@Test
public void College_is_too_long() {
public void throwExceptionWhenSavingTooLongCollegeName() {

// given
String collegeString = "지금이문장은10자임".repeat(20);
Expand All @@ -37,14 +37,14 @@ public void College_is_too_long() {

@DisplayName("College 은 null 일 수 없습니다.")
@Test
public void College_cannot_be_Null() {
public void throwExceptionWhenSavingNullCollegeName() {

assertThrows(InvalidInputException.class, () -> new College(null));
}

@DisplayName("College 은 빈 문자열일 수 없습니다.")
@Test
public void College_cannot_be_Blank() {
public void throwExceptionWhenSavingBlankCollegeName() {

assertThrows(InvalidInputException.class, () -> new College("\t"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class MajorTest {

@DisplayName("Major 타입에 제목을 저장한다.")
@Test
public void Major_is_OK() {
public void saveValidMajorName() {

// given
String majorString = "컴퓨터공학과";
Expand All @@ -26,7 +26,7 @@ public void Major_is_OK() {

@DisplayName("Major 타입에 너무 긴 이름을 저장한다. 50자 이상")
@Test
public void Major_is_too_long() {
public void throwExceptionWhenSavingTooLongMajorName() {

// given
String majorString = "지금이문장은10자임".repeat(50);
Expand All @@ -37,14 +37,14 @@ public void Major_is_too_long() {

@DisplayName("Major 은 null 일 수 없습니다.")
@Test
public void Major_cannot_be_Null() {
public void throwExceptionWhenSavingNullMajorName() {

assertThrows(InvalidInputException.class, () -> new Major(null));
}

@DisplayName("Major 은 빈 문자열일 수 없습니다.")
@Test
public void Major_cannot_be_Blank() {
public void throwExceptionWhenSavingBlankMajorName() {

assertThrows(InvalidInputException.class, () -> new Major("\t"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;

import java.util.ArrayList;
import java.util.List;

import org.springframework.test.util.ReflectionTestUtils;
Expand All @@ -33,26 +32,25 @@ public class MajorInfoServiceTest {

@Mock private MajorInfoRepository majorInfoRepository;

private List<MajorInfo> createSampleMajorInfos() {
MajorInfo majorInfo1 = createMajorInfo("공과대학", "기계공학과", 1);
MajorInfo majorInfo2 = createMajorInfo("자연과학대학", "수학과", 2);
MajorInfo majorInfo3 = createMajorInfo("경영대학", "경영학과", 3);
return List.of(majorInfo1, majorInfo2, majorInfo3);
}

private MajorInfo createMajorInfo(String college, String major, int id) {
MajorInfo majorInfo = new MajorInfo(college, major);
ReflectionTestUtils.setField(majorInfo, "id", id);
return majorInfo;
}

@DisplayName("모든 학과 정보를 불러온다.")
@Test
public void findAllTest() {
public void getAllMajorInfoTest() {

// given
MajorInfo majorInfo1 = new MajorInfo("공과대학", "기계공학과");
MajorInfo majorInfo2 = new MajorInfo("자연과학대학", "수학과");
MajorInfo majorInfo3 = new MajorInfo("경영대학", "경영학과");
ReflectionTestUtils.setField(majorInfo1, "id", 1);
ReflectionTestUtils.setField(majorInfo2, "id", 2);
ReflectionTestUtils.setField(majorInfo3, "id", 3);
List<MajorInfo> majorInfos =
new ArrayList<>() {
{
add(majorInfo1);
add(majorInfo2);
add(majorInfo3);
}
};

List<MajorInfo> majorInfos = createSampleMajorInfos();
given(majorInfoRepository.findAll()).willReturn(majorInfos);

// when
Expand All @@ -68,7 +66,7 @@ public void findAllTest() {

@DisplayName("새로운 학과를 추가한다.")
@Test
public void saveMajorInfoTest() {
public void saveNewMajorInfoTest() {

// given
MajorInfoSaveDto newMajor = new MajorInfoSaveDto("경영대학", "글로벌금융학과");
Expand Down

0 comments on commit d06d5b9

Please sign in to comment.