inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

[인프런 워밍업 클럽 2기 - BE] 3주차 발자국

대협
1

[인프런 워밍업 클럽 2기 - BE] 3주차 발자국

image

이 블로그는 정보근님의 입문자를 위한 Spring Boot with Kotlin - 나만의 포트폴리오 사이트 만들기 강의 기반으로 코드작성과 코드설명을 적었습니다



1. controller test 코드 분석

애너테이션 조사

메서드 분석

 

@Test
@DisplayName("Introductions 조회")
fun testGetIntroductions() {
    val uri = "/api/v1/introductions"

    val mvcResult = performGet(uri)
    val contentAsString = mvcResult.response.getContentAsString(StandardCharsets.UTF_8)
    val jsonArray = JSONArray(contentAsString)

    assertThat(jsonArray.length()).isPositive()
}

 

@Test
@DisplayName("Link 조회")
fun testGetLinks() {
    val uri = "/api/v1/links"

    val mvcResult = performGet(uri)
    val contentAsString = mvcResult.response.getContentAsString(StandardCharsets.UTF_8)
    val jsonArray = JSONArray(contentAsString)

    assertThat(jsonArray.length()).isPositive()
}
@Test
@DisplayName("Resume 조회")
fun testGetResume() {
    val uri = "/api/v1/resume"

    val mvcResult = performGet(uri)
    val contentAsString = mvcResult.response.getContentAsString(StandardCharsets.UTF_8)
    val jsonObject = JSONObject(contentAsString)

    assertThat(jsonObject.optJSONArray("experiences").length()).isPositive()
    assertThat(jsonObject.optJSONArray("achievements").length()).isPositive()
    assertThat(jsonObject.optJSONArray("skills").length()).isPositive()
}
@Test
@DisplayName("Projects 조회")
fun testProjects() {
    val uri = "/api/v1/projects"

    val mvcResult = performGet(uri)
    val contentAsString = mvcResult.response.getContentAsString(StandardCharsets.UTF_8)
    val jsonArray = JSONArray(contentAsString)

    assertThat(jsonArray.length()).isPositive()
}
private fun performGet(uri: String): MvcResult {
    return mockMvc
        .perform(MockMvcRequestBuilders.get(uri))
        .andDo(MockMvcResultHandlers.print())
        .andReturn()
}

2. 부분 코드 분석

<html *lang*="ko" *xmlns:th*="<http://www.thymeleaf.org>" *th:replace*="~{presentation/layouts/layout-main :: layout(~{::#content})}">

 

th:replace="~{presentation/layouts/layout-main :: layout(~{::#content})}"

3. interceptor 코드 분석

@Component
class PresentationInterceptor(
        private val httpInterfaceRepository: HttpInterfaceRepository
) : HandlerInterceptor {
    override fun afterCompletion(request: HttpServletRequest, response: HttpServletResponse, handler: Any, ex: Exception?) {
        val httpInterface = HttpInterface(request)
        httpInterfaceRepository.save(httpInterface)
    }
}

 

@Configuration
class PresentationInterceptorConfiguration(
        private val presentationInterceptor: PresentationInterceptor
) : WebMvcConfigurer {
    override fun addInterceptors(registry: InterceptorRegistry) {
        registry.addInterceptor(presentationInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/assets/**", "/css/**", "/js/**", "/admin/**", "h2**",
                        "/favicon.ico", "/error")
    }
}

[미션4] 조회 REST API 만들기-회고

이번 미션을 하면서 @GetMapping 어노테이션에 대해서는 어느정도 이해를 할 수 있었다. 저번 발자국에서 @Id, @GeneratedValue 어노테이션에 대해서 적었지만 아직 부족하다는 걸 알 수 있었고, @ManyToOne 어노테이션에 대해 더 깊이 공부해야겠다는걸 깨달았다. 아직 Spring에 본질적인걸 이해를 못한걸수도 있는거 같다. 코드를 작성하면 할수록 더 깊게 공부를 해야했고, JAVA 문법도 다시 해야겠다는걸 뼈저리게 느끼면서 한것 같다...


3주차 회고

Spring은 하면 할수록 재밌는건 맞다. Test 코드를 작성할 때도 이래서 이코드가 작동이 되는것도 알 수 있고, 부분적인 걸 따로 모듈화? 해서 만드는것도 재밌다. 이번 워밍업 클럽도 곧 종료가 되는데 java의 중요성도 깨달아서 java공부도 열심히 하고, Spring에 대해서 더 깊게 공부할거다!

참고로 중간점검 때 정보근님께서 내가 작성한 질문에 대해 답변을 해주신거 같다. 나의 질문은 이번 워밍업 클럽이 종료가 되면 java의 정석, spring공부를 할건데 추천해줄 강의가 있냐 라는 질문이었다. 답변은 역시 김영한님의 강의였고 다른 강의도 추천해주셨는데 이거는 확인해보고 글을 수정해야겠다...! 무튼, spring을 선택한건 잘한 일 같다...!

백엔드 워밍업클럽 java kotlin spring Test코드 미션4 회고록 코드설명 백엔드

답변 0