티스토리 뷰

728x90

테스트 중인 클래스에서 이미 한 메서드의 로직을 검증했음에도 

이곳 저곳에서 많이 불려서 쓰이는 경우에 해당 메서드를 모킹하여 복잡도를 줄일 수 있다.

 

해당 방법은 Partial Mocking이라 불리며 Spy Bean을 이용하는 방법이다.

 

테스트 대상을 해당 어노테이션의 순서에 맞게 설정한다.(반드시 Spy가 먼저 와야 한다.)

@Spy
@InjectMocks
SellerService sellerService;

 


@Spy와 @InjectMocks를 같이 쓰는건 위험하지 않을까?

Each annotation has different purposes and they don't step on each other clearly as long as you need to use partial mocks. (a.k.a. stubbing related method(s) that has been already tested and/or trusted)For example, you have a class to test, which has dependency injection(s) that doesn't have to be real so you want to @InjectMocks. Besides, the method you are testing calls another method inside, which was already tested somewhere, or it calls external reference which was also tested most likely independently. So, you don't want to be bothered to test same method(s) more than once, and your test code should not be impacted by the out-of-scope implementation change anytime in the future.Only @Mock & @Spy, or @Mock & @InjectMocks pairs make no sense.

https://stackoverflow.com/questions/38567326/is-it-discouraged-to-use-spy-and-injectmocks-on-the-same-field#:~:text=It%20is%20uncommon%2C%20and%20arguably,InjectMocks%20instance%20with%20those%20fields.

 

Is it discouraged to use @Spy and @InjectMocks on the same field?

In the project I'm working on right now, I often see the @Spy and @InjectMocks used together on a field. I have never seen it this way in any tutorials or other resources. I googled about this spec...

stackoverflow.com

 

Partial Mocking과 같은 명확한 목적이 있다면 문제될게 없는 조합이다.(최소한 기능상으로는)


 

이미 테스트를 마친 메서드를 다음과 같이 선언한다.

doReturn(ReturnMock).when(underTest).methodBeenTested(any());

 

when().thenReturn()은 "실제 메서드를 호출하는 방법"이므로 inner method의 모킹을 위해서 사용할 수 없어 doReturn.when을 사용해야 한다.

 


doReturn().then()과 when().thenReturn()의 차이

 

doReturn().then()은 언제나 사용할 수 있지만
when().thenReturn()은 사용할 수 없는 다음과 경우가 있다.

void를 return하는 메서드, spy를 사용하는 메서드, 같은 메서드를 여러번 호출하는 경우 등

doReturn().then()은 언제나 사용할 수 있는 대신 컴파일 타입 체킹이 되지 않으며 '덜 직관적'인 단점이 있다.

 

https://stackoverflow.com/questions/20353846/mockito-difference-between-doreturn-and-when

 

Mockito - difference between doReturn() and when()

I am currently in the process of using Mockito to mock my service layer objects in a Spring MVC application in which I want to test my Controller methods. However, as I have been reading on the spe...

stackoverflow.com

 

정리하자면 특정한 상황을 제외하고는 when.thenReturn을 쓰는 것이 Readability가 훨씬 좋기에 가능한 한 when.thenReturn을 사용하고 partial mocking 등이 필요한 경우에만 doReturn을 사용하자.


 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함