단순히 테스트를 위해 인터페이스의 메서드 중 일부를 구현하고 싶어했던 시절이 있었다. 심지어 코틀린에서
도 그랬다.
JVM
에서 동적 프락시와 익명 객체, 위임, 선택적인 오버라이딩을 조합해서 다음과 같이 코드를 작
성했다.
inline fun <reified T> fake(): T =
Proxy.newProxyInstance(
T::class.java.classLoader,
arrayOf(T::class.java)
) { _, _, _ ->
TODO(“not implemented”)
} as T
val sentEmails = mutableListOf<Email>()
val testCollaborator: EmailSystem =
object : EmailSystem by fake() {
override fun send(email: Email) {
sentEmails.add(email)
}
}
17.2
그렇지만 실제 목에서 벗어났는가?
이 질문은 좋은 질문이다!
어떤 면에서 이제 막 목 프레임워크를 조악하게 본딴 물건을 만들었다. 파라미터 매처도 없고
메서드를 호출할 수 없을 때, 테스트를 실패시킬 방법도 없으며 실행 순서를 표현할 수 있는 방
법도 없다.
하지만 다른 방법을 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month, and much more.