목표

  • mocking 한 객체들의 메서드 호출들을 모두 로깅하도록 한다.

MockSettings

  • 일반적으로 mockito를 이용해서 목킹할 때 mock(MyClass.class) 형태로 목칭을 하지만, 두번째 인자로 목킹할 때 추가적인 설정을 할 수 있다.
MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer());
MyList listMock = mock(MyList.class, customSettings);
  • MockSettings에는 invocationListeners를 등록할 수 있고, 이는 목킹한 객체에 메서드 호출이 발생할 때 동작하는 리스너다.
List mockWithListener = mock(MyList.class, withSettings().invocationListeners(new YourInvocationListener()));
  • Mockito-Kotlin 도 이를 지원하고 있고, 이를 통해서 호출된 메서드가 무엇인지 로깅할 수 있다.
mock<T>(invocationListeners = arrayOf(  
    InvocationListener {  
       logger.info { "${it.invocation.location} \n${it.invocation}"  }  
    }))

참고 자료