[Test] UserServiceTest 작성#126
Conversation
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public class UserTestRepository implements UserRepository { |
There was a problem hiding this comment.
생성자를 추가해야할 것 같네요.
이 코드에서는 repository안에 데이터가 없을 겁니다. 비어있는 ArrayList인 것이죠. 생성자에서 필요한 데이터들을 ArrayList에 직접 넣어줘야지 아래의 Override한 메서드들이 제대로 테스트 될 것 같아요
| } | ||
|
|
||
| @Test | ||
| void createAccessToken이_정상적으로_작동한다() { |
There was a problem hiding this comment.
createAccessToken을 테스트하는 것은 JwtTokenManager에 대한 Test 클래스에서 진행해야할 것 같아요. UserService에 대한 테스트라면 UserService의 메서드를 테스트하는 것이 좋습니다.
| .build(); | ||
|
|
||
| // @Value 값을 채워주기 위한 임시 값 세팅 | ||
| jwtTokenManager.setValue(secretKey, 1L, 1L); |
There was a problem hiding this comment.
시크릿 키에 대한 건 BeforeEach나 BeforeAll 어노테이션을 사용해 그냥 초기화 해주는게 더 좋아보이네요
| // when | ||
| AuthorizationResponseDto authorizationResponseDto = userService.signup(request, secretKey); | ||
|
|
||
| // then |
There was a problem hiding this comment.
추가로 유저의 refreshToken 필드도 갱신이 되었는지 확인을 해보면 더 좋을 것 같습니다!
| .nickname("nickname") | ||
| .platform(Platform.YOURWEATHER) | ||
| .build(); | ||
| jwtTokenManager.setValue(secretKey, 1L, 1L); |
There was a problem hiding this comment.
이 부분도 마찬가지로 BeforeAll 등의 어노테이션을 사용해서 처음에 초기화해주는게 더 좋을 것 같아요
|
|
||
| // then | ||
| assertEquals(changePasswordResponseDto.isSuccess(), false); | ||
| assertEquals(changePasswordResponseDto.getMessage(), |
There was a problem hiding this comment.
메시지 같은 부분은 수정되기 쉬운 부분이라서 메시지 자체를 비교한다면 리팩토링 내성이 좀 떨어질 것 같아요.
isSuccess 하나만 비교하도록 하는게 더 좋아보이네용 isSuccess 하나만으로도 요청이 실패했는지 성공했는지 알 수 있으니까요!
| public void setValue(String secretKey, Long accessTokenExpiration, Long refreshTokenExpiration) { | ||
| this.secretKey = secretKey; | ||
| this.accessTokenExpiration = accessTokenExpiration; | ||
| this.refreshTokenExpiration = refreshTokenExpiration; | ||
| } |
There was a problem hiding this comment.
이 부분은 secretKey, accessTokenExpiration, refreshTokenExpiration을 직접 필드로 넣지 말고 인터페이스와 객체를 추가해 @Value 어노테이션에 대한 JwtTokenManager의 의존성을 제거하면 하면 굳이 프로덕션 코드에 테스트 전용 메서드를 안 써도 될 것 같아요 자세한건 나중에 적어드릴게요..!
|
추가로 하나 요청드리고 싶은 건 public interface SomeInterface {
String getSecretKey();
String getAccessTokenExpiration();
String getRefreshTokenExpiration();
}
// 운영 코드에서 쓰는 클래스
@Component
public class SomeClass implements SomeInterface {
@Value("${jwt.secretKey}")
private String secretKey;
@Value("${jwt.access.expiration}")
private Long accessTokenExpiration;
@Value("${jwt.refresh.expiration}")
private Long refreshTokenExpiration;
@Override
public String getSecretKey(){
...
}
...
}
// 테스트 코드에서 쓰는 클래스
public class SomeTestClass implement SomeInterface {
private String secretKey = "더미값";
...
@Override
public String getSecretKey() {
...
}
...
}@Component
@Slf4j
public class JwtTokenManager {
private String secretKey;
private Long accessTokenExpiration;
private Long refreshTokenExpiration;
...
public JwtTokenManager(SomeInterface some) {
secretKey = some.getSecretKey();
accessTokenExpiration = some.getAccessTokenExpiration();
refreshTokenExpiration = some.getRefreshTokenExpiration();
}
...
} |
이러면 |

Description
Main Features
Others