Mockito์™€ BDDMockito๋ฅผ ์ด์šฉํ•ด ํ…Œ์ŠคํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ BDDMockito๋ฅผ ์ด์šฉํ•˜๋ฉด,

์ƒํƒœ ๋ณ€ํ™”์— ์ดˆ์ ์„ ๋งž์ถ”๋Š” ํ๋ฆ„์— ์กฐ๊ธˆ ๋” ๊ฐ€๊น๊ฒŒ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

Mockito๋ฅผ ์ด์šฉํ•˜๋ฉด, when().thenReturn() ์„, BDDMockito๋ฅผ ์ด์šฉํ•˜๋ฉด given().willReturn() ์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ Mockito๋“  BDDMocktio๋“  when() ํ˜น์€ given()์„ ํ†ตํ•ด ๋™์ž‘์„ ์ง€์ •ํ•œ๋‹ค.

ํ•˜์ง€๋งŒ, void()๋ฅผ ๋ฆฌํ„ด ํ˜•์‹์œผ๋กœ ๊ฐ–๋Š” ๋ฉ”์†Œ๋“œ๋Š” stubbingํ•˜๋Š” ๋ฒ•์ด ์•ฝ๊ฐ„ ๋‹ค๋ฅด๋‹ค.

์œ„์—์„œ ์„ค๋ช…ํ•œ ์ผ๋ฐ˜ stubbing์€ when(mock.method()).thenReturn(value) ํ˜•์‹์ธ๋ฐ, mock.method()๊ฐ€ void๊ฐ’์„ ๊ฐ€์ง€๋ฉด when(void)์ฒ˜๋Ÿผ ๋˜๊ธฐ ๋•Œ๋ฌธ์— Java ๋ฌธ๋ฒ•์— ๋งž์ง€ ์•Š๋Š”๋‹ค.

๋Œ€์‹  doNothing().when(mock).method()์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•œ๋‹ค.

Mockito์™€ BDDMockito ๊ฐ๊ฐ์„ ์ด์šฉํ•ด์„œ void ๋ฉ”์„œ๋“œ๋ฅผ ํ…Œ์ŠคํŒ…ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ข€ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ๋‹ค.


1. Mockito ์‚ฌ์šฉ ์‹œ


postService์˜ update๋ฉ”์„œ๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€์ˆ˜๋กœ ์„ธ๊ฐ€์ง€ ๊ฐ’์„ ๊ฐ€์ง€๋ฉฐ ๋ฆฌํ„ด ํƒ€์ž…์ด void์ด๋‹ค.

public void update(Integer id, String userName, PostUpdateRequest request) {
    Post post = validatePost(id, userName);
    post.updatePost(request.getTitle(),request.getBody());
}

update ๋ฉ”์„œ๋“œ๋Š” ํฌ์ŠคํŠธ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋ฉฐ ์„ฑ๊ณต ํ…Œ์ŠคํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

ํ•œ๊ฐ€์ง€ ๋‹ฌ๋ผ์ง„ ์ ์€ doNothing() ์„ ์‚ฌ์šฉํ–ˆ๋‹ค๋Š” ์ ์ด๋‹ค.

์„ฑ๊ณต ํ…Œ์ŠคํŠธ์‹œ void ํƒ€์ž…์˜ ๊ฒฝ์šฐ ๋ฐ˜ํ™˜๊ฐ’์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— doNothing() ์œผ๋กœ ๋™์ž‘์˜ ๊ฒฐ๊ณผ๋ฅผ ์ง€์ •ํ•˜๊ณ , ๊ทธ ๋’ค์— ๋™์ž‘์˜ ์กฐ๊ฑด์„ ์„ค์ •ํ•œ๋‹ค.

@Test
@DisplayName("ํฌ์ŠคํŠธ ์ˆ˜์ • ์„ฑ๊ณต")
@WithMockCustomUser
public void post_update_success() throws Exception {

    doNothing().when(postService).update(any(),any(),any());

    mockMvc.perform(put("/api/v1/posts/1")
                    .with(csrf())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(objectMapper.writeValueAsBytes(postUpdateRequest)))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.resultCode").value("SUCCESS"))
            .andExpect(jsonPath("$.result.postId").value(1))
            .andExpect(jsonPath("$.result.message").value("ํฌ์ŠคํŠธ ์ˆ˜์ • ์™„๋ฃŒ"))
            .andDo(print());

    verify(postService, times(1)).update(any(), any(), any());
}

์ด๋ฒˆ์—” void ํƒ€์ž…์˜ ์‹คํŒจ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•ด๋ณด๊ฒ ๋‹ค.

๊ธฐ์กด์˜ when().willThrow() ๊ฐ€ ์•„๋‹ˆ๋ผ doThrow() ๋กœ ์‹œ์ž‘ํ•œ ๋’ค, ๋™์ž‘์˜ ์กฐ๊ฑด์„ ์„ค์ •ํ•œ๋‹ค.

@Test
@DisplayName("ํฌ์ŠคํŠธ ์ˆ˜์ • ์‹คํŒจ(2) : ํฌ์ŠคํŠธ ๋‚ด์šฉ ์—†์Œ")
@WithMockCustomUser
public void post_update_fail2() throws Exception {

    doThrow(new AppException(ErrorCode.POST_NOT_FOUND)).when(postService).update(any(),any(), any());

    mockMvc.perform(put("/api/v1/posts/1")
                    .with(csrf())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(objectMapper.writeValueAsBytes(postUpdateRequest)))
            .andExpect(status().is(ErrorCode.POST_NOT_FOUND.getStatus().value()))
            .andExpect(status().isNotFound())
            .andExpect(jsonPath("$.resultCode").value("ERROR"))
            .andExpect(jsonPath("$.result.errorCode").value("POST_NOT_FOUND"))
            .andExpect(jsonPath("$.result.message").value("ํ•ด๋‹น ํฌ์ŠคํŠธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."))
            .andDo(print());

    verify(postService, times(1)).update(any(), any(), any());
}


2. BDDMockito ์‚ฌ์šฉ ์‹œ


BDDMockito์˜ ๊ฒฝ์šฐ์—๋„ ํฌ๊ฒŒ ๋‹ค๋ฅด์ง€ ์•Š๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํฌ์ŠคํŠธ ์ˆ˜์ • ์„ฑ๊ณต ํ…Œ์ŠคํŠธ์—์„  willDoNothing() ์„ ํ†ตํ•ด ๊ฒฐ๊ณผ๋ฅผ ๋จผ์ € ์ง€์ •ํ•˜๊ณ  ์‹œ์ž‘ํ•œ๋‹ค.

@Test
@DisplayName("ํฌ์ŠคํŠธ ์ˆ˜์ • ์„ฑ๊ณต")
@WithMockCustomUser
public void post_update_success() throws Exception {

    willDoNothing().given(postService).update(any(),any(),any());

    mockMvc.perform(put("/api/v1/posts/1")
                    .with(csrf())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(objectMapper.writeValueAsBytes(postUpdateRequest)))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.resultCode").value("SUCCESS"))
            .andExpect(jsonPath("$.result.postId").value(1))
            .andExpect(jsonPath("$.result.message").value("ํฌ์ŠคํŠธ ์ˆ˜์ • ์™„๋ฃŒ"))
            .andDo(print());

    then(postService).should(times(1)).update(any(), any(), any());
}

์‹คํŒจ ํ…Œ์ŠคํŠธ๋„ Mockito๊ฐ€ ์‚ฌ์šฉํ•œ doThrow ๊ฐ€ ์•„๋‹Œ willThrow() ๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.

@Test
@DisplayName("ํฌ์ŠคํŠธ ์ˆ˜์ • ์‹คํŒจ(2) : ํฌ์ŠคํŠธ ๋‚ด์šฉ ์—†์Œ")
@WithMockCustomUser
public void post_update_fail2() throws Exception {

    willThrow(new AppException(ErrorCode.POST_NOT_FOUND)).given(postService).update(any(),any(), any());

    mockMvc.perform(put("/api/v1/posts/1")
                    .with(csrf())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(objectMapper.writeValueAsBytes(postUpdateRequest)))
            .andExpect(status().is(ErrorCode.POST_NOT_FOUND.getStatus().value()))
            .andExpect(status().isNotFound())
            .andExpect(jsonPath("$.resultCode").value("ERROR"))
            .andExpect(jsonPath("$.result.errorCode").value("POST_NOT_FOUND"))
            .andExpect(jsonPath("$.result.message").value("ํ•ด๋‹น ํฌ์ŠคํŠธ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."))
            .andDo(print());

    then(postService).should(times(1)).update(any(), any(), any());
}

์ถ”๊ฐ€์ ์œผ๋กœ, BDDMockito๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Mockito์˜ verify ๋ถ€๋ถ„๋„ ์กฐ๊ธˆ ๋‹ค๋ฅด๋‹ค.

Mockito

verify(postService, times(1)).update(any(), any(), any());

BDDMockito

then(postService).should(times(1)).update(any(), any(), any());

References

Tags:

Categories:

Date:

Leave a comment