포스트

Private Method를 테스트하라

Spring 프로젝트를 기준으로 Spring-test 라이브러리를 추가해준다.

testImplementation 'org.springframework.boot:spring-boot-starter-test'

private method를 테스트 하기 위해서는 spring test 라이브러리에 있는 **ReflectionTestUtils**가 필요하다.

​

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/util/ReflectionTestUtils.html

ReflectionTestUtils (Spring Framework 5.2.8.RELEASE API)

public abstract class ReflectionTestUtils extends Object ReflectionTestUtils is a collection of reflection-based utility methods for use in unit and integration testing scenarios. There are often times when it would be beneficial to be able to set a non- public field, invoke a non- public setter met…

API에서 제공 목록

                                    ![](/assets/images/posts/222086641858/4195b221bfe4.png?type=w580)

​

여기서 private method메서드를 호출해야 하니 invokeMethod 메서드를 사용해서 하겠다.

​

class Target {
 private String name;
 public Target(String name) {
  this.name = name;
 }

 private String addString(String value) {
   return name + value;
 }
}

class TestTarget {

  @Test
  void privateMethod() {
   Target target = new Target("ABC");
   String returnValue = ReflectionTestUtils.invokeMethod(target, "addString", "DEF")
   org.junit.jupiter.api.Assertions.assertEquals("ABCDEF", returnValue);
  }
}

​

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.