Skip to content

Commit

Permalink
#119 test: userRepository 테스트 코드 작성
Browse files Browse the repository at this point in the history
- findByUserToken()
- existsByEmail()
- findByEmail()
- findAllByUserToken()
- findAllByUserId()
  • Loading branch information
rivkode committed Sep 5, 2024
1 parent ade3d9c commit 746521e
Showing 1 changed file with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package com.seoultech.synergybe.domain.user;

import com.seoultech.synergybe.domain.common.CustomPasswordEncoder;
import com.seoultech.synergybe.domain.user.repository.UserRepository;
import com.seoultech.synergybe.domain.user.vo.UserEmail;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootTest
@Transactional
public class UserRepositoryTest {
@Autowired
private UserRepository userRepository;

@Autowired
private CustomPasswordEncoder passwordEncoder;

@Test
void findByUserToken() {
// given
String userToken = "usr_token";
User user = User.builder()
.id(1L)
.userToken(userToken)
.email("[email protected]")
.password("password")
.major("major")
.name("name")
.passwordEncoder(passwordEncoder)
.build();
userRepository.save(user);

// when
User savedUser = userRepository.findByUserToken(userToken).orElseThrow();

// then
assertThat(savedUser.getUserToken()).isEqualTo(userToken);
}

@Test
void existsByEmail() {
// given
String userEmail = "[email protected]";
User user = User.builder()
.id(1L)
.userToken("userToken")
.email(userEmail)
.password("password")
.major("major")
.name("name")
.passwordEncoder(passwordEncoder)
.build();
userRepository.save(user);
UserEmail userEmailEmbed = new UserEmail(userEmail);

// when
boolean isExist = userRepository.existsByEmail(userEmailEmbed);

// then
assertThat(isExist).isTrue();
}

@Test
void findByEmail() {
String userEmail = "[email protected]";
User user = User.builder()
.id(1L)
.userToken("userToken")
.email(userEmail)
.password("password")
.major("major")
.name("name")
.passwordEncoder(passwordEncoder)
.build();
userRepository.save(user);

// when
User savedUser = userRepository.findByEmail(userEmail);

// then
assertThat(savedUser).isNotNull();
}

@Test
void findAllByUserToken() {
String userToken = "usr_token";
String userToken2 = "usr_token2";
User user = User.builder()
.id(1L)
.userToken(userToken)
.email("[email protected]")
.password("password")
.major("major")
.name("name")
.passwordEncoder(passwordEncoder)
.build();

User user2 = User.builder()
.id(2L)
.userToken(userToken2)
.email("[email protected]")
.password("password")
.major("major")
.name("name")
.passwordEncoder(passwordEncoder)
.build();
userRepository.save(user);
userRepository.save(user2);

// when
List<User> users = userRepository.findAllByUserToken(List.of(userToken, userToken2));

// then
assertThat(users.size()).isEqualTo(2);
}

@Test
void findAllByUserId() {
Long userId = 1L;
Long userId2 = 2L;
User user = User.builder()
.id(userId)
.userToken("userToken")
.email("[email protected]")
.password("password")
.major("major")
.name("name")
.passwordEncoder(passwordEncoder)
.build();

User user2 = User.builder()
.id(userId2)
.userToken("userToken2")
.email("[email protected]")
.password("password")
.major("major")
.name("name")
.passwordEncoder(passwordEncoder)
.build();
userRepository.save(user);
userRepository.save(user2);

// when
List<User> users = userRepository.findAllByUserId(List.of(userId, userId2));

// then
assertThat(users.size()).isEqualTo(2);
}



}

0 comments on commit 746521e

Please sign in to comment.