7. 특정 포스트를 불러오기 특정 포스트 불러오기 요청 처리하기
PostController 
@GetMapping("/post/{postId}") public  String getPostDetails (@PathVariable("postId") Long id, Model model) {    Post  post  =  postService.getPostById(id);     PostDto  postDto  =  PostDto.builder()             .id(post.getId())             .name(post.getName())             .title(post.getTitle())             .content(post.getContent())             .writeTime(post.getWriteTime())             .build();     model.addAttribute("postDto" , postDto);     return  "details" ; } 
 
PostControllerTest.java 
@Test @DisplayName("상세 페이지를 가져온다.") public  void  getPostDetails ()  throws  Exception{    Post  mockPost  =  Post.builder()             .id(1L )             .name("tester" )             .title("test" )             .content("test" )             .writeTime(LocalDateTime.now())             .build();     given(postService.getPostById(any())).willReturn(mockPost);     ResultActions  resultActions  =  mockMvc.perform(get("/post/1" ));     resultActions             .andExpect(status().isOk());     verify(postService).getPostById(any()); } 
 
PostControllerTest.java 
@Test @DisplayName("상세 페이지를 못 가져온다.") public  void  getPostDetailsException ()  throws  Exception{    given(postService.getPostById(any())).willThrow(new  PostNotExistedException (1L ));     ResultActions  resultActions  =  mockMvc.perform(get("/post/1" ));     resultActions             .andExpect(status().isNotFound());     verify(postService).getPostById(any()); } 
 
포스트가 없는 경우에 대한 예외처리하기 BoardErrorAdvice.java 
@ControllerAdvice public  class  BoardErrorAdvice  {    @ResponseBody      @ResponseStatus(HttpStatus.NOT_FOUND)      @ExceptionHandler(PostNotExistedException.class)      public  void  handleNotFound () {     } } 
 
PostNotExistedException.java 
public  class  PostNotExistedException  extends  RuntimeException {    public  PostNotExistedException (Long id) {         super ("Post id : "  + id + " is not Existed" );     } } 
 
PostService.java 
public  Post getPostById (Long id)  {    Post  post  =  postRepository.findById(id).orElseThrow(() -> new  PostNotExistedException (id));     return  post; } 
 
PostServiceTest.java 
@Test @DisplayName("특정 포스트를 가져온다.") public  void  getPostById () {    Post  mockPost  =  Post.builder()             .id(1L )             .name("tester" )             .title("test" )             .content("test" )             .writeTime(LocalDateTime.now())             .build();     given(postRepository.findById(1L )).willReturn(Optional.of(mockPost));     Post  post  =  postService.getPostById(1L );     verify(postRepository).findById(1L );     assertThat(post.getId()).isEqualTo(mockPost.getId());     assertThat(post.getTitle()).isEqualTo(mockPost.getTitle());     assertThat(post.getName()).isEqualTo(mockPost.getName());     assertThat(post.getContent()).isEqualTo(mockPost.getContent());     assertThat(post.getWriteTime()).isEqualTo(mockPost.getWriteTime()); } @Test @DisplayName("특정 포스트를 못 가져온다.") public  void  getPostByIdException () {    Post  mockPost  =  Post.builder()             .id(1L )             .name("tester" )             .title("test" )             .content("test" )             .writeTime(LocalDateTime.now())             .build();     given(postRepository.findById(1L )).willThrow(new  PostNotExistedException (1L ));          assertThatThrownBy(() ->{         postService.getPostById(1L );     }).isInstanceOf(PostNotExistedException.class);     verify(postRepository).findById(1L ); }