-
Notifications
You must be signed in to change notification settings - Fork 305
/
ReflectionTest.java
131 lines (99 loc) · 4.08 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
package reflection;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
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("");
assertThat(clazz.getName()).isEqualTo("");
assertThat(clazz.getCanonicalName()).isEqualTo("");
}
@Test
void givenClassName_whenCreatesObject_thenCorrect() throws ClassNotFoundException {
final Class<?> clazz = Class.forName("reflection.Question");
assertThat(clazz.getSimpleName()).isEqualTo("");
assertThat(clazz.getName()).isEqualTo("");
assertThat(clazz.getCanonicalName()).isEqualTo("");
}
@Test
void givenObject_whenGetsFieldNamesAtRuntime_thenCorrect() {
final Object student = new Student();
final Field[] fields = null;
final List<String> actualFieldNames = null;
assertThat(actualFieldNames).contains("name", "age");
}
@Test
void givenClass_whenGetsMethods_thenCorrect() {
final Class<?> animalClass = Student.class;
final Method[] methods = null;
final List<String> actualMethods = null;
assertThat(actualMethods)
.hasSize(3)
.contains("getAge", "toString", "getName");
}
@Test
void givenClass_whenGetsAllConstructors_thenCorrect() {
final Class<?> questionClass = Question.class;
final Constructor<?>[] constructors = null;
assertThat(constructors).hasSize(2);
}
@Test
void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception {
final Class<?> questionClass = Question.class;
final Constructor<?> firstConstructor = null;
final Constructor<?> secondConstructor = null;
final Question firstQuestion = null;
final Question secondQuestion = null;
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 = null;
assertThat(fields).hasSize(0);
}
@Test
void givenClass_whenGetsDeclaredFields_thenCorrect() {
final Class<?> questionClass = Question.class;
final Field[] fields = null;
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 = null;
assertThat(field.getName()).isEqualTo("questionId");
}
@Test
void givenClassField_whenGetsType_thenCorrect() throws Exception {
final Field field = Question.class.getDeclaredField("questionId");
final Class<?> fieldClass = null;
assertThat(fieldClass.getSimpleName()).isEqualTo("long");
}
@Test
void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception {
final Class<?> studentClass = Student.class;
final Student student = null;
final Field field = null;
// todo field에 접근 할 수 있도록 만든다.
assertThat(field.getInt(student)).isZero();
assertThat(student.getAge()).isZero();
field.set(null, null);
assertThat(field.getInt(student)).isEqualTo(99);
assertThat(student.getAge()).isEqualTo(99);
}
}