forked from woowacourse/java-mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReflectionTest.java
133 lines (102 loc) · 4.83 KB
/
ReflectionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package reflection;
import static org.assertj.core.api.Assertions.assertThat;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
class ReflectionTest {
private static final Logger log = LoggerFactory.getLogger(ReflectionTest.class);
@Test
void givenObject_whenGetsClassName_thenCorrect() {
final Class<Question> clazz = Question.class;
assertThat(clazz.getSimpleName()).isEqualTo("Question");
assertThat(clazz.getName()).isEqualTo("reflection.Question");
assertThat(clazz.getCanonicalName()).isEqualTo("reflection.Question");
}
@Test
void givenClassName_whenCreatesObject_thenCorrect() throws ClassNotFoundException {
final Class<?> clazz = Class.forName("reflection.Question");
assertThat(clazz.getSimpleName()).isEqualTo("Question");
assertThat(clazz.getName()).isEqualTo("reflection.Question");
assertThat(clazz.getCanonicalName()).isEqualTo("reflection.Question");
}
@Test
void givenObject_whenGetsFieldNamesAtRuntime_thenCorrect() {
final Object student = new Student();
final Field[] fields = student.getClass().getDeclaredFields();
final List<String> actualFieldNames = Arrays.stream(fields).map(Field::getName).toList();
assertThat(actualFieldNames).contains("name", "age");
}
@Test
void givenClass_whenGetsMethods_thenCorrect() {
final Class<?> animalClass = Student.class;
final Method[] methods = animalClass.getDeclaredMethods();
final List<String> actualMethods = Arrays.stream(methods).map(Method::getName).toList();
assertThat(actualMethods)
.hasSize(3)
.contains("getAge", "toString", "getName");
}
@Test
void givenClass_whenGetsAllConstructors_thenCorrect() {
final Class<?> questionClass = Question.class;
final Constructor<?>[] constructors = questionClass.getConstructors();
assertThat(constructors).hasSize(2);
}
@Test
void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception {
final Class<?> questionClass = Question.class;
final Constructor<?> firstConstructor = questionClass.getDeclaredConstructors()[0];
final Constructor<?> secondConstructor = questionClass.getDeclaredConstructors()[1];
final Question firstQuestion = (Question) firstConstructor.newInstance("gugu", "제목1", "내용1");
final Question secondQuestion = (Question) secondConstructor.newInstance(1L, "gugu", "제목2", "내용2", new Date(), 1);
assertThat(firstQuestion.getWriter()).isEqualTo("gugu");
assertThat(firstQuestion.getTitle()).isEqualTo("제목1");
assertThat(firstQuestion.getContents()).isEqualTo("내용1");
assertThat(secondQuestion.getWriter()).isEqualTo("gugu");
assertThat(secondQuestion.getTitle()).isEqualTo("제목2");
assertThat(secondQuestion.getContents()).isEqualTo("내용2");
}
@Test
void givenClass_whenGetsPublicFields_thenCorrect() {
final Class<?> questionClass = Question.class;
final Field[] fields = questionClass.getFields();
assertThat(fields).hasSize(0);
}
@Test
void givenClass_whenGetsDeclaredFields_thenCorrect() {
final Class<?> questionClass = Question.class;
final Field[] fields = questionClass.getDeclaredFields();
assertThat(fields).hasSize(6);
assertThat(fields[0].getName()).isEqualTo("questionId");
}
@Test
void givenClass_whenGetsFieldsByName_thenCorrect() throws Exception {
final Class<?> questionClass = Question.class;
final Field field = questionClass.getDeclaredField("questionId");
assertThat(field.getName()).isEqualTo("questionId");
}
@Test
void givenClassField_whenGetsType_thenCorrect() throws Exception {
final Field field = Question.class.getDeclaredField("questionId");
final Class<?> fieldClass = field.getType();
assertThat(fieldClass.getSimpleName()).isEqualTo("long");
}
@Test
void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
final Class<?> studentClass = Student.class;
final Student student = (Student) studentClass.getConstructor().newInstance();
final Field field = studentClass.getDeclaredField("age");
// todo field에 접근 할 수 있도록 만든다.
field.setAccessible(true);
assertThat(field.getInt(student)).isZero();
assertThat(student.getAge()).isZero();
field.set(student, 99);
assertThat(field.getInt(student)).isEqualTo(99);
assertThat(student.getAge()).isEqualTo(99);
}
}