Skip to content

Commit

Permalink
test: PostControllerTest createPost가 성공하는 경우 200을 반환하는지 테스트 (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
birdieHyun committed Jun 18, 2023
1 parent 2cc4bb3 commit a0c16c1
Showing 1 changed file with 72 additions and 4 deletions.
76 changes: 72 additions & 4 deletions be/src/test/java/com/secondhand/post/PostControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package com.secondhand.post;

import com.secondhand.post.dto.CreatePostResponseDto;
import com.secondhand.post.dto.PostSaveDto;
import com.secondhand.post.dto.SearchCondition;
import com.secondhand.user.entity.User;
import com.secondhand.user.login.JwtUtil;
import com.secondhand.user.login.dto.LoggedInUser;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.util.Collections;

import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -23,6 +33,7 @@ class PostControllerTest {

@MockBean
private PostService postService;

@MockBean
private JwtUtil jwtUtil;

Expand All @@ -37,11 +48,68 @@ void getPostTest() throws Exception {

// when // then
mockMvc.perform(MockMvcRequestBuilders.get("/posts")
.param("page", String.valueOf(pageable.getPageNumber()))
.param("size", String.valueOf(pageable.getPageSize()))
.param("category", String.valueOf(searchCondition.getCategory()))
.param("region", String.valueOf(searchCondition.getRegion())))
.param("page", String.valueOf(pageable.getPageNumber()))
.param("size", String.valueOf(pageable.getPageSize()))
.param("category", String.valueOf(searchCondition.getCategory()))
.param("region", String.valueOf(searchCondition.getRegion())))
.andExpect(status().isOk())
.andExpect(jsonPath("$.message").value("메인 화면 조회 성공"));
}

@Test
@DisplayName("판매글을 정상적으로 생성하고 200 상태 코드를 반환하는지 확인한다.")
void createPostTest() throws Exception {
// given
PostSaveDto postSaveDto = getPostSaveDto();

User user = User.builder()
.id(2L)
.githubId(12345L)
.loginId("exampleLoginId")
.profileUrl("exampleProfileUrl")
.firstRegionId(1)
.build();

LoggedInUser mockUser = new LoggedInUser(user);

CreatePostResponseDto mockResponse = new CreatePostResponseDto(1L);

// when interceptor 에서 검증 통과하도록 mocking
when(postService.createPost(Mockito.any(), Mockito.any())).thenReturn(mockResponse);

when(jwtUtil.validateTokenIsManipulated(anyString())).thenReturn(true);
when(jwtUtil.validateTokenIsExpired(anyString())).thenReturn(true);
when(jwtUtil.extractedUserFromToken(anyString())).thenReturn(mockUser);

// then
mockMvc.perform(MockMvcRequestBuilders.multipart("/posts")
.file(mockMultipartFile())
.param("title", postSaveDto.getTitle())
.param("regionId", String.valueOf(postSaveDto.getRegionId()))
.param("categoryId", String.valueOf(postSaveDto.getCategoryId()))
.param("badgeId", String.valueOf(postSaveDto.getBadgeId()))
.param("price", String.valueOf(postSaveDto.getPrice()))
.param("content", postSaveDto.getContent())
.header("Authorization", "Bearer token")
.sessionAttr("loggedInUser", mockUser))
.andExpect(status().isOk())
.andExpect(jsonPath("$.status").value("success"))
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("판매글 생성 성공"))
.andExpect(jsonPath("$.data.id").value(1L));
}

private MockMultipartFile mockMultipartFile() {
return new MockMultipartFile(
"photos", "sample.jpg", "image/jpeg", "sample-image".getBytes()
);
}

private PostSaveDto getPostSaveDto() {
return new PostSaveDto(
"Sample Title", 1, 1, 1, 10000L,
"Sample Content",
Collections.singletonList(mockMultipartFile())
);
}
}

0 comments on commit a0c16c1

Please sign in to comment.