-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
367 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ploy/src/main/java/com/example/seminar/dto/request/member/MemberProfileUpdateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
spring-code-for-deploy/src/test/java/com/example/seminar/config/RestDocsSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.example.seminar.config; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.restdocs.RestDocumentationExtension; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
@ExtendWith(RestDocumentationExtension.class) | ||
public abstract class RestDocsSupport { | ||
|
||
protected MockMvc mockMvc; | ||
|
||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...g-code-for-deploy/src/test/java/com/example/seminar/controller/ControllerTestManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.seminar.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
|
||
@WebMvcTest | ||
@ActiveProfiles("test") | ||
public class ControllerTestManager { | ||
|
||
@Autowired | ||
MockMvc mockMvc; | ||
|
||
@Autowired | ||
ObjectMapper objectMapper; | ||
} |
95 changes: 95 additions & 0 deletions
95
...ng-code-for-deploy/src/test/java/com/example/seminar/controller/MemberControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package com.example.seminar.controller; | ||
|
||
import com.example.seminar.domain.Part; | ||
import com.example.seminar.domain.SOPT; | ||
import com.example.seminar.dto.request.member.MemberCreateRequest; | ||
import com.example.seminar.dto.request.member.MemberProfileUpdateRequest; | ||
import com.example.seminar.dto.response.MemberGetResponse; | ||
import com.example.seminar.service.member.MemberService; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.BDDMockito; | ||
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.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
import java.net.URI; | ||
import java.util.List; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
@WebMvcTest(controllers = MemberController.class) | ||
@ActiveProfiles("test") | ||
public class MemberControllerTest extends ControllerTestManager { | ||
|
||
@MockBean | ||
private MemberService memberService; | ||
@Test | ||
@DisplayName("신규 회원을 등록한다.") | ||
void createMember() throws Exception { | ||
|
||
// given | ||
when(memberService.create(any(MemberCreateRequest.class))) | ||
.thenReturn("/api/member/1"); | ||
|
||
MemberCreateRequest request = new MemberCreateRequest( | ||
"오해영", | ||
"5hae0", | ||
28, | ||
SOPT.builder() | ||
.part(Part.DESIGN) | ||
.build() | ||
); | ||
|
||
// when, then | ||
mockMvc.perform(MockMvcRequestBuilders.post("/api/member") | ||
.content(objectMapper.writeValueAsString(request)) | ||
.contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isCreated()) | ||
.andExpect(MockMvcResultMatchers.header().string("Location", "/api/member/1")) | ||
; | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 정보 목록을 조회한다.") | ||
void getMembersProfile() throws Exception { | ||
mockMvc.perform(MockMvcRequestBuilders.get("/api/member")) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isOk()); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 정보를 수정한다.") | ||
void updateMemberSoptInfo() throws Exception { | ||
|
||
MemberProfileUpdateRequest request = new MemberProfileUpdateRequest((short) 6, Part.SERVER); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.patch("/api/member/1") | ||
.content(objectMapper.writeValueAsString(request)) | ||
.contentType(MediaType.APPLICATION_JSON_VALUE)) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isNoContent()); | ||
} | ||
|
||
@Test | ||
@DisplayName("회원 정보를 삭제한다.") | ||
void deleteMember() throws Exception { | ||
// given // when // then | ||
mockMvc.perform(MockMvcRequestBuilders.delete("/api/member/1")) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isNoContent()); | ||
|
||
} | ||
|
||
|
||
} |
54 changes: 54 additions & 0 deletions
54
spring-code-for-deploy/src/test/java/com/example/seminar/controller/PostControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.example.seminar.controller; | ||
|
||
import com.example.seminar.dto.request.post.PostCreateRequest; | ||
import com.example.seminar.service.post.PostService; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
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.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
@WebMvcTest(controllers = PostController.class) | ||
@ActiveProfiles("test") | ||
public class PostControllerTest extends ControllerTestManager { | ||
|
||
private static final String POST_API_ENDPOINT = "/api/posts"; | ||
|
||
@MockBean | ||
PostService postService; | ||
|
||
@Test | ||
@DisplayName("게시글을 생성할 수 있다.") | ||
void createPost() throws Exception { | ||
// given | ||
when(postService.create(any(PostCreateRequest.class), any(Long.class))) | ||
.thenReturn("1"); | ||
|
||
PostCreateRequest request = new PostCreateRequest( | ||
"제목", | ||
"내용" | ||
); | ||
|
||
String CUSTOM_USER_ID = "X-Auth-Id"; | ||
|
||
// when then | ||
mockMvc.perform( | ||
MockMvcRequestBuilders.post(POST_API_ENDPOINT) | ||
.content(objectMapper.writeValueAsString(request)) | ||
.contentType(MediaType.APPLICATION_JSON_VALUE) | ||
.header(CUSTOM_USER_ID, 1L)) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isCreated()) | ||
.andExpect(MockMvcResultMatchers.header().string("Location", "/api/posts/1")); | ||
} | ||
} |
Oops, something went wrong.