-
Notifications
You must be signed in to change notification settings - Fork 305
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[MVC 구현하기 - 1단계] 에버(손채영) 미션 제출합니다. #694
Changes from 1 commit
df6fa40
cb172db
6e4c0e8
f0cda4b
2ecca46
e312664
4e45a35
e76390e
aa674d8
d4d274b
0118167
ba17506
de7370e
613a0c1
5b297ce
54f3b36
fdd3078
8ace1d8
6afede4
279fc92
21fcb37
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,72 @@ | ||
package reflection; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class Junit3TestRunner { | ||
|
||
Class<Junit3Test> clazz = Junit3Test.class; | ||
|
||
@Test | ||
void run() throws Exception { | ||
Class<Junit3Test> clazz = Junit3Test.class; | ||
|
||
// TODO Junit3Test에서 test로 시작하는 메소드 실행 | ||
Junit3Test junit3Test = clazz.getConstructor().newInstance(); | ||
List<Method> testMethods = getDeclaredTestMethods(clazz); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 학습) getDeclaredXX()와 getxx()의 차이는 무엇일까요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
관련하여 학습한 코드도 커밋해두었습니다! de7370e |
||
assertThat(testMethods).hasSize(2); | ||
for (Method method : testMethods) { | ||
method.invoke(junit3Test); | ||
} | ||
} | ||
|
||
private List<Method> getDeclaredTestMethods(Class<Junit3Test> clazz) { | ||
Method[] methods = clazz.getDeclaredMethods(); | ||
Method[] methods = clazz.getMethods(); | ||
return Arrays.stream(methods) | ||
.filter(method -> method.getName().startsWith("test")) | ||
.toList(); | ||
} | ||
|
||
@DisplayName("getMethods: 상속 메서드를 포함한 모든 public 메서드를 반환한다.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍 |
||
@Test | ||
void getMethods() { | ||
// given & when | ||
Method[] methods = clazz.getMethods(); | ||
|
||
// then | ||
List<String> actual = Arrays.stream(methods) | ||
.map(Method::getName) | ||
.toList(); | ||
assertThat(actual).containsExactly( | ||
"test1", | ||
"test2", | ||
"equals", | ||
"toString", | ||
"hashCode", | ||
"getClass", | ||
"notify", | ||
"notifyAll", | ||
"wait", | ||
"wait", | ||
"wait"); | ||
} | ||
|
||
@DisplayName("getDeclaredMethods: 상속 메서드를 제외하고 직접 선언된 모든 메서드를 반환한다.") | ||
@Test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍👍 |
||
void getDeclaredMethods() { | ||
// given & when | ||
Method[] methods = clazz.getDeclaredMethods(); | ||
|
||
// then | ||
List<String> actual = Arrays.stream(methods) | ||
.map(Method::getName) | ||
.toList(); | ||
assertThat(actual).containsExactly( | ||
"test1", | ||
"test2", | ||
"privateTest3"); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
학습) Reflection이란 무엇이고, 어떻게 활용할 수 있나요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reflection은 런타임의 실행 중인 자바 프로그램이 자신을 조사하거나 프로그램의 내부 속성을 조작하는 API입니다.
Reflection은 추상화를 깨는 등의 단점을 가지고 있기 때문에, 애플리케이션 단보다는 보통 프레임워크나 라이브러리를 만들 때 활용되고, 런타임에 알아낼 수 있는 정보를 통해 컴파일 타임에 작업을 할 경우 사용하기 좋습니다!
저희가 흔히 사용하는 라이브러리 중 활용되는 예시로는, JUnit 라이브러리에서 @test 어노테이션이 붙은 메서드만 실행하는 작업, Spring이 자동으로 의존성을 주입하는 작업 등이 있습니다 :)