수정 테스트 코드 

@Test
    public void 게시물_제목_변경() throws Exception {
        // given
        Long targetPostId = 1L;
        PostDTO post = postService.getPost(targetPostId);

        PostDTO postDTO = PostDTO.builder()
                .title("title update")
                .contents(post.getContents())
                .build();

        // when
        ResultActions perform = mockMvc.perform(put("/post/{id}", targetPostId)
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(postDTO)));

        // then
        perform
                .andExpect(jsonPath("message").exists())
                .andExpect(jsonPath("message").value("success"))
                .andExpect(status().isOk())
                .andDo(print());

    }

    @Test
    public void 게시물_내용_변경() throws Exception {
        // given
        Long targetPostId = 1L;
        PostDTO post = postService.getPost(targetPostId);

        PostDTO postDTO = PostDTO.builder()
                .title(post.getTitle())
                .contents("contents update")
                .build();


        // when
        ResultActions perform = mockMvc.perform(put("/post/{id}", targetPostId)
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(postDTO)));

        // then
        perform
                .andExpect(jsonPath("message").exists())
                .andExpect(jsonPath("message").value("success"))
                .andExpect(status().isOk())
                .andDo(print());
    }

    @Test
    public void 게시물_제목_내_변경() throws Exception {
        // given
        Long targetPostId = 1L;
        PostDTO post = postService.getPost(targetPostId);

        PostDTO postDTO = PostDTO.builder()
                .title(post.getTitle())
                .title("title update")
                .contents("contents update")
                .build();


        // when
        ResultActions perform = mockMvc.perform(put("/post/{id}", targetPostId)
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(postDTO)));

        // then
        perform
                .andExpect(jsonPath("message").exists())
                .andExpect(jsonPath("message").value("success"))
                .andExpect(status().isOk())
                .andDo(print());
    }

제목만 수정하였을경우, 내용만 수정하였을경우, 둘다 수정하였을경우의 케이스를 모두 테스트 하였습니다.


삭제 테스트 코드

@Test
    public void 게시물_삭제() throws Exception {
        // given
        Long targetPostId = 1L;

        // when
        ResultActions perform = mockMvc.perform(delete("/post/{id}", targetPostId));

        // then
        perform.andExpect(status().isOk())
                .andExpect(jsonPath("message").exists())
                .andExpect(jsonPath("message").value("success"))
                .andDo(print());
    }

 

+ Recent posts