Open YeahyunKim opened 2 months ago
UserTest
package com.sparta.javafeed.entity;
import com.sparta.javafeed.dto.SignupRequestDto; import com.sparta.javafeed.dto.UserInfoRequestDto; import com.sparta.javafeed.enums.UserStatus; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import static org.junit.jupiter.api.Assertions.*;
class UserTest { private User user; private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
// @BeforeEach는 테스트 코드가 실행되기 전에 실행시켜주는 어노테이션이다. 유저 테스트를 진행하기 위해 먼저 유저를 생성해야하기 때문에, setUser() 메서드에 @BeforeEach를 걸어준다. @BeforeEach void setUser() { SignupRequestDto requestDto = SignupRequestDto.builder() .accountId("insidesy1234") .password("1q2w3e4r!@#$") .email("insidesy1234@gmail.com") .name("김예현") .build(); String encodedPassword = passwordEncoder.encode(requestDto.getPassword()); user = new User(requestDto, encodedPassword); } @Test @DisplayName("[updateUserStatus()] 업데이트 UserStatus") void updateUserStatus() { // given UserStatus userStatus = UserStatus.ACTIVE; // when user.updateUserStatus(userStatus); //then assertEquals(userStatus, user.getUserStatus()); } @Test @DisplayName("[saveRefreshToken()] 리프레시 토큰 저장") void saveRefreshToken() { //given String refreshToken = "RefreshToken"; //when user.saveRefreshToken(refreshToken); //then assertEquals(refreshToken, user.getRefreshToken()); } @Test @DisplayName("[checkRefreshToken()] 리프레시 토큰 체크") void checkRefreshToken() { //given String refreshToken = "RefreshToken"; user.saveRefreshToken(refreshToken); //when Boolean isValidToken = refreshToken.equals(user.getRefreshToken()); //then assertTrue(isValidToken); } @Test @DisplayName("[updateUserInfo()] 유저 정보 업데이트") void updateUserInfo() { //given UserInfoRequestDto infoRequestDto = new UserInfoRequestDto("김예현2", "insidesy9@gmail.com", "개발공부는 어렵습니다."); //when user.updateUserInfo(infoRequestDto); //then assertEquals(infoRequestDto.getName(), user.getName()); assertEquals(infoRequestDto.getEmail(), user.getEmail()); assertEquals(infoRequestDto.getIntro(), user.getIntro()); } @Test @DisplayName("[updatePassword()] 비밀번호 변경") void updatePassword() { // given String newPassword = "QWER!@#$1234"; String encodedNewPassword = passwordEncoder.encode(newPassword); // when user.updatePassword(encodedNewPassword); // then assertTrue(passwordEncoder.matches(newPassword, user.getPassword())); }
}
<br> 2. `NewsfeedTest` 코드 추가 ```java package com.sparta.javafeed.entity; import com.sparta.javafeed.dto.NewsfeedRequestDto; import com.sparta.javafeed.dto.SignupRequestDto; import com.sparta.javafeed.dto.UserInfoRequestDto; import com.sparta.javafeed.enums.ErrorType; import com.sparta.javafeed.exception.CustomException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import static org.junit.jupiter.api.Assertions.*; class NewsfeedTest { private Newsfeed newsfeed; private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); // @BeforeEach는 테스트 코드가 실행되기 전에 실행시켜주는 어노테이션이다. 뉴스피드 테스트를 진행하기 위해 먼저 유저를 생성하고 뉴스피드를 생성해야하기 때문에, setNewsfeed() 메서드에 @BeforeEach를 걸어준다. @BeforeEach void setNewsfeed() { User user = setUser(); NewsfeedRequestDto requestDto = NewsfeedRequestDto.builder() .title("자바의 정석") .description("자바는 객체지향 프로그래밍 언어 OOP 이다.") .build(); Newsfeed testNewsfeed = new Newsfeed(requestDto.getTitle(), requestDto.getDescription(), user); } private User setUser() { SignupRequestDto requestDto = SignupRequestDto.builder() .accountId("insidesy1234") .password("1q2w3e4r!@#$") .email("insidesy1234@gmail.com") .name("김예현") .build(); String encodedPassword = passwordEncoder.encode(requestDto.getPassword()); return new User(requestDto, encodedPassword); } @Test @DisplayName("[update()] 업데이트 뉴스피드") void update() { //given NewsfeedRequestDto requestDto = new NewsfeedRequestDto("파이썬의 정석", "파이썬으로 갈아탈까?"); //when newsfeed.update(requestDto); //then assertEquals(requestDto.getTitle(), newsfeed.getTitle()); assertEquals(requestDto.getDescription(), newsfeed.getDescription()); } @Test @DisplayName("[userValidate()] 유저 검증 성공") void userValidateSuccess() { //given User user = setUser(); //when,then newsfeed.userValidate(user); } @Test @DisplayName("[userValidate()] 유저 검증 실패") void userValidateFail() { // given User user = setUser(); UserInfoRequestDto requestDto = UserInfoRequestDto.builder() .name("김예현2") .build(); user.updateUserInfo(requestDto); // when, then CustomException exception = assertThrows(CustomException.class, () -> newsfeed.userValidate(user)); assertEquals(ErrorType.NO_AUTHENTICATION, exception.getErrorType()); } }
CommentTest
import com.sparta.javafeed.dto.CommentRequestDto; import com.sparta.javafeed.dto.NewsfeedRequestDto; import com.sparta.javafeed.dto.SignupRequestDto; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
class CommentTest { private Comment comment;
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); // @BeforeEach는 테스트 코드가 실행되기 전에 실행시켜주는 어노테이션 @BeforeEach void setComment() { User testUser = setUser(); testUser.setId(1L); Newsfeed testNewsfeed = setNewsfeed(); CommentRequestDto requestDto = CommentRequestDto.builder() .description("제가 자바를 공부해봐도 괜찮을까요?") .build(); comment = new Comment(testUser, testNewsfeed, requestDto.getDescription()); } private Newsfeed setNewsfeed() { User testUser = setUser(); NewsfeedRequestDto requestDto = NewsfeedRequestDto.builder() .title("자바의 정석") .description("자바는 객체지향 프로그래밍 언어 OOP 이다.") .build(); return new Newsfeed(requestDto.getTitle(), requestDto.getDescription(), testUser); } private User setUser() { SignupRequestDto requestDto = SignupRequestDto.builder() .accountId("insidesy1234") .password("1q2w3e4r!@#$") .email("insidesy1234@gmail.com") .name("김예현") .build(); String encodedPassword = passwordEncoder.encode(requestDto.getPassword()); return new User(requestDto, encodedPassword); } @Test @DisplayName("[validate()] 사용자 인증 성공") void validateSuccess() { //given User testUser = setUser(); testUser.setId(1L); //when, then comment.validate(testUser); } @Test @DisplayName("[update()] 댓글 수정") void update() { // given String newDescription = "자바는 너무 어려워요"; // when comment.update(newDescription); // then assertEquals(newDescription, comment.getDescription()); } @Test @DisplayName("[increaseLikeCnt()] 좋아요 증가") void increaseLikeCnt() { // given int increaseCount = 8; // when for (int i = 0; i < increaseCount; i++) { comment.increaseLikeCnt(); } // then assertEquals(increaseCount, comment.getLikeCnt()); } @Test @DisplayName("[decreaseLikeCnt()] 좋아요 감소") void decreaseLikeCnt() { // given int increaseCount = 4; int decreaseCount = 2; for (int i = 0; i < increaseCount; i++) { comment.increaseLikeCnt(); } // when for (int i = 0; i < decreaseCount; i++) { comment.decreaseLikeCnt(); } // then assertEquals(increaseCount - decreaseCount, comment.getLikeCnt()); }
<br> 4. `NewsfeedRequestDtoTest` 코드 추가 ```java package com.sparta.javafeed.dto; import com.sparta.javafeed.entity.Newsfeed; import com.sparta.javafeed.entity.User; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import static org.junit.jupiter.api.Assertions.*; class NewsfeedRequestDtoTest { private NewsfeedRequestDto requestDto; @BeforeEach void setNewsfeedRequestDto() { requestDto = new NewsfeedRequestDto("자바의 정석", "자바는 재밌다"); } private User setUser() { SignupRequestDto requestDto = SignupRequestDto.builder() .accountId("insidesy1234") .password("1q2w3e4r!@#$") .email("insidesy1234@gmail.com") .name("김예현") .build(); PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String encodedPassword = passwordEncoder.encode(requestDto.getPassword()); return new User(requestDto, encodedPassword); } @Test @DisplayName("[toEntity()] Entity 생성") void toEntity() { // given User user = setUser(); // when Newsfeed newsfeed = requestDto.toEntity(user); // then assertEquals(user, newsfeed.getUser()); assertEquals(requestDto.getTitle(), newsfeed.getTitle()); assertEquals(requestDto.getDescription(), newsfeed.getDescription()); } }
NewsfeedResponseDtoTest
package com.sparta.javafeed.dto;
import com.sparta.javafeed.entity.Newsfeed; import com.sparta.javafeed.entity.User; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
class NewsfeedResponseDtoTest { private NewsfeedResponseDto newsfeedResponseDto;
@BeforeEach void setNewsfeedResponseDto() { Newsfeed testNewsfeed = setNewsfeed(); newsfeedResponseDto = new NewsfeedResponseDto(testNewsfeed); } private Newsfeed setNewsfeed() { User testUser = setUser(); NewsfeedRequestDto requestDto = NewsfeedRequestDto.builder() .title("자바의 정석") .description("자바는 객체지향 프로그래밍 언어 OOP 이다.") .build(); return new Newsfeed(requestDto.getTitle(), requestDto.getDescription(), testUser); } private User setUser() { SignupRequestDto requestDto = SignupRequestDto.builder() .accountId("insidesy1234") .password("1q2w3e4r!@#$") .email("insidesy1234@gmail.com") .name("김예현") .build(); PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String encodedPassword = passwordEncoder.encode(requestDto.getPassword()); return new User(requestDto, encodedPassword); } @Test @DisplayName("[toDto()] ResponseDto로 반환") void toDtoSuccess() { // given Newsfeed newsfeed = setNewsfeed(); // when NewsfeedResponseDto responseDto = NewsfeedResponseDto.toDto(newsfeed); // then assertEquals(newsfeedResponseDto.getAccountId(), responseDto.getAccountId()); assertEquals(newsfeedResponseDto.getTitle(), responseDto.getTitle()); assertEquals(newsfeedResponseDto.getDescription(), responseDto.getDescription()); }
<br> 6. 결과 1) `UserTest` ![image](https://github.com/YeahyunKim/SpartaNbCamp-Javafeed-TestCode-pp/assets/132278619/b359a4c3-24b5-4827-aacb-73a4250bd0d6) <br> 2) `NewsfeedTest` ![image](https://github.com/YeahyunKim/SpartaNbCamp-Javafeed-TestCode-pp/assets/132278619/5b749312-3da7-4177-9464-2893f0bfc3d9) <br> 3) `CommentTest` ![image](https://github.com/YeahyunKim/SpartaNbCamp-Javafeed-TestCode-pp/assets/132278619/439561cd-1b03-4e64-9611-024de3515c19) <br> 4) `NewsfeedRequestDtoTest` ![image](https://github.com/YeahyunKim/SpartaNbCamp-Javafeed-TestCode-pp/assets/132278619/dc12bab0-9243-4fdf-b2dd-15db15f1916c) <br> 5) `NewsfeedResponseDtoTest` ![image](https://github.com/YeahyunKim/SpartaNbCamp-Javafeed-TestCode-pp/assets/132278619/2e0cdcdb-1c9e-4016-8f3b-0afa7a103ddf)
UserTest
코드 추가import com.sparta.javafeed.dto.SignupRequestDto; import com.sparta.javafeed.dto.UserInfoRequestDto; import com.sparta.javafeed.enums.UserStatus; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import static org.junit.jupiter.api.Assertions.*;
class UserTest { private User user; private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
}
CommentTest
코드 추가import com.sparta.javafeed.dto.CommentRequestDto; import com.sparta.javafeed.dto.NewsfeedRequestDto; import com.sparta.javafeed.dto.SignupRequestDto; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import static org.junit.jupiter.api.Assertions.*;
class CommentTest { private Comment comment;
}
NewsfeedResponseDtoTest
코드 추가import com.sparta.javafeed.entity.Newsfeed; import com.sparta.javafeed.entity.User; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import static org.junit.jupiter.api.Assertions.*;
class NewsfeedResponseDtoTest { private NewsfeedResponseDto newsfeedResponseDto;
}