You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
회원가입 기능에 대하여 테스트 케이스를 작성하였다. 중복된 이메일로 회원가입을 시도하는 경우, 정상적으로 회원가입이 이루어지도록 하였다.
@SpringBootTest
public class UserServiceTest {
@Autowired
private UserService userService;
@Autowired
private UserRepository userRepository;
@Test
public void testRegisterUser() {
UserRegisterDto existingUser = new UserRegisterDto("[email protected]", "password", "STUDENT");
userRepository.save(new User(existingUser.getEmail(), existingUser.getPassword(), existingUser.getRole()));
UserRegisterDto newUser = new UserRegisterDto("[email protected]", "newpassword", "STUDENT");
Assertions.assertThrows(ResponseStatusException.class, () -> {
userService.registerUser(newUser);
});
UserRegisterDto uniqueUser = new UserRegisterDto("[email protected]", "password", "STUDENT");
userService.registerUser(uniqueUser);
User savedUser = userRepository.findByEmail("[email protected]").orElseThrow();
Assertions.assertEquals("[email protected]", savedUser.getEmail());
}
}
로그인 기능에서 유효성검증 + JWT 토큰 생성 로직을 추가하여 테스트를 통과하는 코드를 작성하였다.
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final JwtTokenProvider jwtTokenProvider;
public String loginUser(UserLoginDto loginDto) {
User user = userRepository.findByEmail(loginDto.getEmail())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid credentials"));
if (!passwordEncoder.matches(loginDto.getPassword(), user.getPassword())) {
throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, "Invalid credentials");
}
return jwtTokenProvider.createToken(user.getEmail(), user.getRole());
}
}
The text was updated successfully, but these errors were encountered:
1. TDD 에 대한 이해
TDD란?
TDD 는 테스트 주도 개발로 기본적인 절차는 테스트 작성 —> 기능 구현 —> 리팩토링 단계로 이루어진다 !
**
TDD는 언제 필요할까? 왜 필요한가?
2. 수강신청 서비스 구현
필수 구현 사항
3. 나의 이해와 의견
The text was updated successfully, but these errors were encountered: