JUnit 테스트 - 본인계좌목록보기 컨트롤러

jeongjin-kim·2023년 7월 19일

JUnit5

목록 보기
9/11
post-thumbnail

최주호 강사님의 인프런 강좌 정리 및 실습한 기록

목표

  • 특정 유저의 계좌 목록을 확인하는 컨트롤러 작성

구현목록

@GetMapping 작성에 관하여

@PathVariable 사용하는 건에 관하여

아래의 코드에 문제가 있는가?


// 
@GetMapping("/account/{id}")
public ResponseEntity<?> findAppUserAccount(
        @PathVariable Long id,
        @AuthenticationPrincipal LoginAppUser loginAppUser
) {

    // 인증에 성공한 유저의 id, 요청을 하는 유저의 id 를 검증한다.
    if (!Objects.equals(id, loginAppUser.getAppUser().getId())) {
        throw new CustomForbiddenException("권한이 없습니다.");
    }

    AccountListResponseDTO accountListResponseDTO =
            accountService.selectAccountByAppUserId(id);
    return new ResponseEntity<>(new ResponseDTO<>(1, "id: " + id + "유저의 계좌를 찾았습니다.", accountListResponseDTO), HttpStatus.OK);
}

id 를 받으면 권한을 해당 유저에 대해서 확인하는 로직이 필요하다. 근데 이미 인증이 된 상태임을 고려하면 id 를 받아서 처리하는 의미가 없다.


@GetMapping("/account/login-user")
public ResponseEntity<?> findAppUserAccount(
        @AuthenticationPrincipal LoginAppUser loginAppUser
) {

    AccountListResponseDTO accountListResponseDTO =
            accountService.selectAccountByAppUserId(loginAppUser.getAppUser().getId());
    return new ResponseEntity<>(new ResponseDTO<>(1, "id: " + loginAppUser.getAppUser().getUsername()+ " 유저 계좌를 찾았습니다.", accountListResponseDTO), HttpStatus.OK);
}

테스트

처음으로 나온 숙제, 직접 테스트 코드를 작성해보아라.

0개의 댓글