λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

개발/Spring

Spring 톡합 ν…ŒμŠ€νŠΈ - JUnit5

restAPI ꡬ쑰둜 requestλŠ” @valid 둜 μœ νš¨μ„± 검사λ₯Ό μ§„ν–‰ν•˜κ³  μžˆλŠ” ν”„λ‘œμ νŠΈκ°€ μžˆλ‹€

더 μ•ˆμ •μ μΈ μ„œλ²„ μ œκ³΅μ„ μœ„ν•΄ API 개발이 λͺ¨λ‘ μ™„λ£Œλœ μ‹œμ μ—μ„œ ν…ŒμŠ€νŠΈλ₯Ό μ§„ν–‰ν•˜λ €κ³  ν•œλ‹€

 

κ°œλ°œκΈ°κ°„μ΄ 짧아

κ°œλ°œμ„ ν•˜λ©° λ‹¨μœ„ ν…ŒμŠ€νŠΈλ₯Ό μ§„ν–‰ν•˜λŠ” TDD λ°©μ‹μœΌλ‘œ κ°œλ°œν•˜μ§€ μ•Šμ•˜κΈ° λ•Œλ¬Έμ—

μ΄μ œμ™€μ„œ λ‹¨μœ„ ν…ŒμŠ€νŠΈλ₯Ό 일일히 μ§„ν–‰ν•˜κΈ°λ³΄λ‹€ 톡합 ν…ŒμŠ€νŠΈλ₯Ό ν•˜λŠ” 것이 더 μ ν•©ν•˜λ‹€κ³  νŒλ‹¨ν–ˆλ‹€

 

post / body 예제

import ...

@SpringBootTest
@AutoConfigureMockMvc
class MissionControllerTest {

    @Autowired
    MockMvc mockMvc;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    @WithUserDetails(value = "1232142μ–΄μ©Œκ΅¬userUID")
    @Transactional
    void createOrPatchMissionSuccess() throws Exception {

        String content = objectMapper.writeValueAsString(new MissionInfo(ZonedDateTime.now(), "test"));

        mockMvc.perform(post("/v2/mission/info")
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(content))
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0000"))
                .andDo(print());
    }
}

get 예제

@Test
@WithUserDetails(value = "라라라라ㅏ userUID")
@Transactional
void getMissionSuccessRecordSuccess() throws Exception {

    mockMvc.perform(get("/v2/mission/success"))
            .andExpect(status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0000"))
            .andDo(print());
}

patch / param 예제

@Test
@WithUserDetails(value = "V8Inlz8q67hhClVGD9gJeRDQ7hY2")
@Transactional
void changeMissionSuccessRecordSuccess() throws Exception {

    mockMvc.perform(patch("/v2/mission/success")
                    .param("id", "1"))
            .andExpect(status().isOk())
            .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0000"))
            .andDo(print());
}

delete 예제 

    @Test
    @WithUserDetails(value = "ㅇㄴ미ㅓㄹ아ㅣㅏuserUID")
    @Transactional
    void deleteAccountSuccess() throws Exception {
        mockMvc.perform(delete("/v2/accounts"))
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers.jsonPath("$.code").value("0000"))
                .andDo(print());
    }

 

 

 

μ°Έκ³ ν•œ 자료

 

Spring Boot - μŠ€ν”„λ§ λΆ€νŠΈ ν†΅ν•©ν…ŒμŠ€νŠΈ 방법과 팁(Spring boot Integration Test)

Spring Boot 테스트 이번 ν¬μŠ€νŒ…μ—μ„œλŠ” Spring Bootμ—μ„œ ν†΅ν•©ν…ŒμŠ€νŠΈν•˜λŠ” 방법에 λŒ€ν•΄μ„œ μ•Œμ•„λ³΄λ €κ³  ν•©λ‹ˆλ‹€. https://medium.com/@ssowonny/%EC%84%A4%EB%A7%88-%EC%95%84%EC%A7%81%EB%8F%84-%ED%85%8C%EC%8A..

galid1.tistory.com

 

JUnit μ‚¬μš©

 

[JUnit] μŠ€ν”„λ§λΆ€νŠΈ + junit5 ν™˜κ²½μ—μ„œ MockMvc둜 컨트둀러 ν…ŒμŠ€νŠΈν•˜κΈ°

Mockμ΄λž€?  μ‚¬μ „적 의미둜 'ν…ŒμŠ€νŠΈλ₯Ό μœ„ν•΄ λ§Œλ“  λͺ¨ν˜•'을 μ˜λ―Έν•˜κ³ , ν…ŒμŠ€νŠΈλ₯Ό μœ„ν•΄ μ‹€μ œ 객체와 λΉ„μŠ·ν•œ λͺ¨μ˜ 객체λ₯Ό λ§Œλ“œλŠ” 것을 λͺ¨ν‚Ή(Mocking), λͺ¨ν‚Ήν•œ 객체λ₯Ό λ©”λͺ¨λ¦¬μ—μ„œ μ–»μ–΄λ‚΄λŠ” 과정을 λͺ©μ—…(Mock-

scshim.tistory.com

 

Junit 정리 - MockMvcλ₯Ό μ΄μš©ν•œ 컨트둀러 ν…ŒμŠ€νŠΈ

μž…μ‚¬ ν›„ κ°œλ°œμ„ ν•œμ§€ 벌써 3λ…„ μ°¨κ°€ λ˜μ—ˆλ‹€. κ°œλ°œμ„ 처음 μ ‘ν•  λ•ŒλŠ” μ–΄λ–»κ²Œ κ΅¬ν˜„ν•΄μ•Ό 할지 μ–΄λ–»κ²Œ λ§Œλ“€μ–΄μ•Ό ν•˜λŠ”μ§€μ— λŒ€ν•œ 관심이 더 μ»Έλ‹€. ν•˜μ§€λ§Œ μš”μƒˆλŠ” 개발 ν›„ μ–΄λ–»κ²Œ ν…ŒμŠ€νŠΈλ₯Ό μ§„ν–‰ν•˜μ—¬ λ¨Ό

wedul.site

 

λ‹¨μœ„ ν…ŒμŠ€νŠΈμ— Spring Securiy 인증 κ΄€λ ¨ μ–΄λ…Έν…Œμ΄μ…˜λ“€ μ‚¬μš©ν•˜κΈ°

πŸ” λ“€μ–΄κ°€λ©° μ•ˆλ…•ν•˜μ„Έμš”! 이번 ν¬μŠ€νŒ…μ—μ„œλŠ” ν…ŒμŠ€νŠΈν•  λ•Œ Spring Security 인증을 ν•  수 있게 μ œκ³΅λ˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜λ“€μ„ μ†Œκ°œν•˜λ €κ³  ν•©λ‹ˆλ‹€. 이 ν¬μŠ€νŒ…μ—μ„œ λ‹€λ£¨λŠ” μ–΄λ…Έν…Œμ΄μ…˜ λͺ©λ‘μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆ

shinsunyoung.tistory.com

 

λ°˜μ‘ν˜•