아무래도 public한 녀석만을 테스트하다가 private한 녀석은 테스트를 하지 못해서 아쉬움이 남았다. 근데 해결할 방법이 있다니!
참고한 블로그의 예시
JUnitTestClass jUnitTestClass = new JUnitTestClass();
Method method = jUnitTestClass.getClass().getDeclaredMethod("add", int.class, int.class);
method.setAccessible(true);
int ret = (int) method.invoke(jUnitTestClass, 1,2);
프로젝트의 예시
public static final String METHOD_NAME = "createProfile";
private OAuth2AuthenticationSuccessHandler oAuth2AuthenticationSuccessHandler;
private Method createProfileMethod;
private AwsStorageProperties awsStorageProperties;
@BeforeEach
void beforeEach() throws Exception {
awsStorageProperties = new AwsStorageProperties(FactoryPreset.STORAGE);
oAuth2AuthenticationSuccessHandler = new OAuth2AuthenticationSuccessHandler(
null, profileRepository, null, null, awsStorageProperties);
createProfileMethod = oAuth2AuthenticationSuccessHandler.getClass().getDeclaredMethod(METHOD_NAME);
createProfileMethod.setAccessible(true);
}
예시
JUnitTestClass jUnitTestClass = new JUnitTestClass();
Field field = jUnitTestClass.getClass().getDeclaredField("myVariable");
field.setAccessible(true);
int value = (int)field.get(jUnitTestClass);