reflection

java core 2017. 4. 12. 10:03

private 함수를 테스트 할 경우, java의 reflection기능을 사용해서 test할 수 있다.


import java.lang.reflect.Method;


Map<String, Long> repeatMap = null;
try {
Method method = ChatRankingService.class.getDeclaredMethod("getRepeatCount", List.class);
method.setAccessible(true);
repeatMap = (Map<String, Long>)method.invoke(chatRankingService, serviceCodes);
} catch (Exception e) {
}

이와 같이 method.setAccessible(true)를 하면 private함수도 실행이 가능하다. 

Posted by yongary
,