Skip to content

Commit

Permalink
Test: MemberRepository Test 코드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Taejin1221 committed Sep 2, 2024
1 parent aa98c2e commit 0a701cc
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
testImplementation 'com.h2database:h2'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

// JWT Token Dependency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static org.junit.jupiter.api.Assertions.*;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.DisplayName;
Expand All @@ -10,6 +12,7 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.transaction.annotation.Transactional;

import kaboo.kaboo_auth.domain.Course;
import kaboo.kaboo_auth.domain.entity.Member;

@DataJpaTest
Expand Down Expand Up @@ -58,4 +61,66 @@ void findByUsername_Failure() {
// Then
assertEquals(result, Optional.empty());
}

@Test
@DisplayName("DB에 존재하는 classNum 찾을 때 findByClassNum method 테스트")
void findByClassNum_Success() {
// Given
Member member1 = Member.builder().englishName("Alice").classNum(1).build();
Member member2 = Member.builder().englishName("Bob").classNum(1).build();

memberRepository.save(member1);
memberRepository.save(member2);

List<Member> expectedMembers = Arrays.asList(member1, member2);

// When
List<Member> byClassNum = memberRepository.findByClassNum(1);

// Then
assertEquals(byClassNum.size(), 2, "리스트의 크기가 다릅니다.");
assertTrue(byClassNum.containsAll(expectedMembers), "리스트에 모든 멤버가 포함되어 있지 않습니다.");
}

@Test
@DisplayName("DB에 존재하지 않는 classNum 찾을 때 findByClassNum method 테스트")
void findByClassNum_Failure() {
// Given

// When
List<Member> byClassNum = memberRepository.findByClassNum(1);

// Then
assertEquals(byClassNum.size(), 0);
}

@Test
@DisplayName("findByKoreaName 성공 테스트")
void findByKoreaName_Success() {
// Given
Member member = Member.builder()
.koreaName("홍길동")
.classNum(1)
.course(Course.AI)
.build();
memberRepository.save(member);

// When
Optional<Member> byKoreaName = memberRepository.findByKoreaName("홍길동");

// Then
assertEquals(byKoreaName.get(), member);
}

@Test
@DisplayName("findByKoreaName 실패 테스트")
void findByKoreaName_Failure() {
// Given

// When
Optional<Member> byKoreaName = memberRepository.findByKoreaName("홍길동");

// Then
assertTrue(byKoreaName.isEmpty());
}
}

0 comments on commit 0a701cc

Please sign in to comment.