Skip to content

Commit

Permalink
refactor: 테스트 메서드명 수정 및 메서드 삭제
Browse files Browse the repository at this point in the history
  • Loading branch information
mcodnjs committed Nov 6, 2023
1 parent 9eb56e7 commit 43c41d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
4 changes: 2 additions & 2 deletions backend/src/docs/asciidoc/docs.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
| 서버 에러
|===

=== Exception Codes
=== Exception codes
[[exception-codes]]
include::{snippets}/exception-code-controller-test/error-code-documentation/exception-response-fields.adoc[]
include::{snippets}/exception-code-controller-test/get-exception-codes/exception-response-fields.adoc[]

== 여행 API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import hanglog.global.exception.ExceptionCode;
import hanglog.global.exception.ExceptionResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -33,17 +34,17 @@ public class ExceptionCodeControllerTest extends ControllerTest {

@DisplayName("작성된 커스텀 예외 코드를 반환한다.")
@Test
void errorCodeDocumentation() throws Exception {
void getExceptionCodes() throws Exception {
// when & then
final MvcResult mvcResult = mockMvc.perform(RestDocumentationRequestBuilders.get("/errors"))
.andExpect(status().isOk())
.andDo(restDocs.document(
exceptionResponseFields(
new ExceptionResponseFieldsSnippet(
"exception-response",
enumConvertFieldDescriptor(ExceptionCode.values())
convertExceptionCodesToFieldDescriptors(ExceptionCode.values()),
true
)
))
.andReturn();
)).andReturn();

final Map<String, ExceptionResponse> exceptionResponses = objectMapper.readValue(
mvcResult.getResponse().getContentAsString(),
Expand All @@ -54,23 +55,12 @@ void errorCodeDocumentation() throws Exception {
assertThat(exceptionResponses.size()).isEqualTo(ExceptionCode.values().length);
}

private FieldDescriptor[] enumConvertFieldDescriptor(final ExceptionCode[] exceptionCodes) {
return Arrays.stream(exceptionCodes)
private List<FieldDescriptor> convertExceptionCodesToFieldDescriptors(final ExceptionCode[] exceptionCodes) {
return Arrays.stream(Arrays.stream(exceptionCodes)
.map(code -> fieldWithPath(code.name()).description(code.getMessage())
.attributes(
key("code").value(code.getCode()),
key("message").value(code.getMessage()))
).toArray(FieldDescriptor[]::new);
}

public static ExceptionResponseFieldsSnippet exceptionResponseFields(
final String type,
final FieldDescriptor... descriptors
) {
return new ExceptionResponseFieldsSnippet(
type,
Arrays.asList(descriptors),
true
);
).toArray(FieldDescriptor[]::new)).toList();
}
}

0 comments on commit 43c41d9

Please sign in to comment.