-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MockMVC 기반
- Loading branch information
Showing
3 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
wingle/src/test/java/kr/co/wingle/util/ControllerTest.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,26 @@ | ||
package kr.co.wingle.util; | ||
|
||
import org.junit.jupiter.api.Disabled; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@Disabled | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) | ||
@AutoConfigureMockMvc | ||
public abstract class ControllerTest { | ||
|
||
@Autowired | ||
protected ObjectMapper mapper; | ||
|
||
@Autowired | ||
protected MockMvc mockMvc; | ||
|
||
protected String createJson(Object dto) throws JsonProcessingException { | ||
return mapper.writeValueAsString(dto); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
wingle/src/test/java/kr/co/wingle/util/RestDocsConfig.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,31 @@ | ||
package kr.co.wingle.util; | ||
|
||
import static org.springframework.restdocs.snippet.Attributes.*; | ||
|
||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; | ||
import org.springframework.restdocs.operation.preprocess.Preprocessors; | ||
|
||
@TestConfiguration | ||
public class RestDocsConfig { | ||
|
||
@Bean | ||
public RestDocumentationResultHandler write() { | ||
return MockMvcRestDocumentation.document( | ||
// 조각이 생성되는 디렉토리 명을 클래스명/메서드 명으로 정함 | ||
"{class-name}/{method-name}", | ||
// json pretty하게 출력 | ||
Preprocessors.preprocessRequest(Preprocessors.prettyPrint()), | ||
Preprocessors.preprocessResponse(Preprocessors.prettyPrint()) | ||
); | ||
} | ||
|
||
// 제약조건 | ||
public static final Attribute field( | ||
final String key, | ||
final String value) { | ||
return new Attribute(key, value); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
wingle/src/test/java/kr/co/wingle/util/RestDocsTestSupport.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,48 @@ | ||
package kr.co.wingle.util; | ||
|
||
import static org.springframework.restdocs.payload.PayloadDocumentation.*; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.restdocs.RestDocumentationContextProvider; | ||
import org.springframework.restdocs.RestDocumentationExtension; | ||
import org.springframework.restdocs.mockmvc.MockMvcRestDocumentation; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler; | ||
import org.springframework.restdocs.payload.FieldDescriptor; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
import org.springframework.web.filter.CharacterEncodingFilter; | ||
|
||
@Disabled | ||
@Import(RestDocsConfig.class) | ||
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class}) | ||
public class RestDocsTestSupport extends ControllerTest { | ||
|
||
@Autowired | ||
protected RestDocumentationResultHandler restDocs; | ||
|
||
@BeforeEach | ||
void setUp(final WebApplicationContext context, | ||
final RestDocumentationContextProvider provider) { | ||
this.mockMvc = MockMvcBuilders.webAppContextSetup(context) | ||
.apply(MockMvcRestDocumentation.documentationConfiguration(provider)) // rest docs 설정 주입 | ||
.alwaysDo(MockMvcResultHandlers.print()) // andDo(print()) 코드 포함 | ||
.alwaysDo(restDocs) // pretty 패턴과 문서 디렉토리 명 정해준것 적용 | ||
.addFilters(new CharacterEncodingFilter("UTF-8", true)) // 한글 깨짐 방지 | ||
.build(); | ||
} | ||
|
||
protected RestDocumentationResultHandler getResponseFields(FieldDescriptor[] data) { | ||
return restDocs.document( | ||
responseFields(beneathPath("data") // response의 data 필드 하위 내용 문서화 | ||
.withSubsectionId("data") // response-fields-data.adoc 파일 생성 | ||
, data) | ||
); | ||
} | ||
|
||
} |