본문 바로가기

테스트4

(나만을 위한)댓글 생성 메소드 테스트 구현 고민과정 @Transactional public Long createPostComment(Long postId, Long userId, PostCommentRequest createDTO) { PostComment postComment = buildPostComment(userId, postId, createDTO); if (createDTO.getParentCommentId() != null) { // 대댓글일시 PostComment parentComment = postCommentRepository.findById(createDTO.getParentCommentId()) .orElseThrow(() -> new CustomException(HttpStatus.NOT_FOUND, ResultCode.POST_C.. 2024. 4. 7.
테스트코드 작성시 유념사항 Repoistory -> Service -> Controller 순으로 개발한다. 다른 계층에 대한 의존성이 거의 없기 때문에 접근 방법 가짜로 구현하기: 최대한 빨리 테스트를 통과하기 위해 정답이 아닌 가짜 정답을 구현하는 방법 삼각측량법: 값이 다른 여러 테스트를 작성하고, 이를 일반화하여 정답을 구현하는 방법 명백하게 구현하기: 정답을 바로 구현하는 방법 테스트를 위해 구현 설계가 변경될 수 있다. 테스트 코드는 구현의 보조 수단이 절대 아니다. 항상 같은 결과가 반환되는 함(순수함수) 가 테스트하기 좋은 코드 나쁜 테스트 코드 제어할 수 없는 값에 의존하는 경우, Random(), new Date() (LocalDate.now()) 와 같이 실행할때마다 결과가 다른 함수에 의존하는 경우 외부에 영.. 2024. 4. 7.
[Trasactional] 자바 스케줄러 테스트 시도 과정 요구사항: 게임이 시작하면 Room 상태를 PROGRESS로 바꾸고, 1분 뒤에 FINISH로 바꿔라 서비스단 코드 @Service @RequiredArgsConstructor public class RoomUpdateService { private final UserRepository userRepository; private final UserRoomRepository userRoomRepository; private final RoomRepository roomRepository; private final UserService userService; private final RoomService roomService; private final SchedulerService schedulerServi.. 2024. 3. 28.
통합, 단위 테스트 차이 어노테이션 설명 부모 클래스 Bean @SpringBootTest 통합테스트, 전체 IntegrationTest Bean 전체 @WebMvcTest 단위테스트, MVC테스트 MockApiTest MVC관련된 Bean @DataJpaTest 단위테스트, Jpa테스트 RepositoryTest JPA 관련 Bean None 단위테스트, Service테스트 MockTest None None POJO, 도메인 테스트 None None 통합 테스트 장점 모든 Bean을 올리고 테스트를 진행하기 때문에 쉽게 테스트 진행 가능 모든 Bean을 올리고 테스트를 진행하기 때문에 운영환경과 가장 유사하게 테스트 가능 API를 테스트할 경우 요청부터 응답까지 전체적인 테스트 진행 가능 단점 모든 Bean을 올리고 테스트를 .. 2023. 10. 13.