11package reflection ;
22
3- import org .junit .jupiter .api .Test ;
4- import org .slf4j .Logger ;
5- import org .slf4j .LoggerFactory ;
3+ import static org .assertj .core .api .Assertions .assertThat ;
64
75import java .lang .reflect .Constructor ;
86import java .lang .reflect .Field ;
97import java .lang .reflect .Method ;
8+ import java .util .Arrays ;
9+ import java .util .Date ;
1010import java .util .List ;
11-
12- import static org .assertj .core .api .Assertions .assertThat ;
11+ import java .util .stream .Collectors ;
12+ import org .junit .jupiter .api .Test ;
13+ import org .slf4j .Logger ;
14+ import org .slf4j .LoggerFactory ;
1315
1416class ReflectionTest {
1517
1618 private static final Logger log = LoggerFactory .getLogger (ReflectionTest .class );
1719
1820 @ Test
1921 void givenObject_whenGetsClassName_thenCorrect () {
22+ // given
2023 final Class <Question > clazz = Question .class ;
2124
22- assertThat (clazz .getSimpleName ()).isEqualTo ("" );
23- assertThat (clazz .getName ()).isEqualTo ("" );
24- assertThat (clazz .getCanonicalName ()).isEqualTo ("" );
25+ // when, then
26+ assertThat (clazz .getSimpleName ()).isEqualTo ("Question" );
27+ assertThat (clazz .getName ()).isEqualTo ("reflection.Question" );
28+ assertThat (clazz .getCanonicalName ()).isEqualTo ("reflection.Question" );
2529 }
2630
2731 @ Test
2832 void givenClassName_whenCreatesObject_thenCorrect () throws ClassNotFoundException {
33+ // given
2934 final Class <?> clazz = Class .forName ("reflection.Question" );
3035
31- assertThat (clazz .getSimpleName ()).isEqualTo ("" );
32- assertThat (clazz .getName ()).isEqualTo ("" );
33- assertThat (clazz .getCanonicalName ()).isEqualTo ("" );
36+ // when, then
37+ assertThat (clazz .getSimpleName ()).isEqualTo ("Question" );
38+ assertThat (clazz .getName ()).isEqualTo ("reflection.Question" );
39+ assertThat (clazz .getCanonicalName ()).isEqualTo ("reflection.Question" );
3440 }
3541
3642 @ Test
3743 void givenObject_whenGetsFieldNamesAtRuntime_thenCorrect () {
44+ // given
3845 final Object student = new Student ();
39- final Field [] fields = null ;
40- final List <String > actualFieldNames = null ;
46+ final Field [] fields = student .getClass ().getDeclaredFields ();
4147
48+ // when
49+ final List <String > actualFieldNames = Arrays .stream (fields )
50+ .map (Field ::getName )
51+ .collect (Collectors .toList ());
52+
53+ // then
4254 assertThat (actualFieldNames ).contains ("name" , "age" );
4355 }
4456
4557 @ Test
4658 void givenClass_whenGetsMethods_thenCorrect () {
59+ // given
4760 final Class <?> animalClass = Student .class ;
48- final Method [] methods = null ;
49- final List <String > actualMethods = null ;
61+ final Method [] methods = animalClass .getDeclaredMethods ();
62+
63+ // when
64+ final List <String > actualMethods = Arrays .stream (methods )
65+ .map (Method ::getName )
66+ .collect (Collectors .toList ());
5067
68+ // then
5169 assertThat (actualMethods )
52- .hasSize (3 )
53- .contains ("getAge" , "toString" , "getName" );
70+ .hasSize (3 )
71+ .contains ("getAge" , "toString" , "getName" );
5472 }
5573
5674 @ Test
5775 void givenClass_whenGetsAllConstructors_thenCorrect () {
76+ // given
5877 final Class <?> questionClass = Question .class ;
59- final Constructor <?>[] constructors = null ;
6078
79+ // when
80+ final Constructor <?>[] constructors = questionClass .getDeclaredConstructors ();
81+
82+ // then
6183 assertThat (constructors ).hasSize (2 );
6284 }
6385
6486 @ Test
6587 void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect () throws Exception {
88+ // given
6689 final Class <?> questionClass = Question .class ;
67-
68- final Constructor <?> firstConstructor = null ;
69- final Constructor <?> secondConstructor = null ;
70-
71- final Question firstQuestion = null ;
72- final Question secondQuestion = null ;
73-
90+ final Constructor <?> firstConstructor =
91+ questionClass .getDeclaredConstructor (String .class , String .class , String .class );
92+ final Constructor <?> secondConstructor =
93+ questionClass .getDeclaredConstructor (
94+ long .class ,
95+ String .class ,
96+ String .class ,
97+ String .class ,
98+ Date .class ,
99+ int .class
100+ );
101+
102+ // when
103+ final Question firstQuestion =
104+ (Question ) firstConstructor .newInstance ("gugu" , "제목1" , "내용1" );
105+ final Question secondQuestion =
106+ (Question ) secondConstructor .newInstance (1L , "gugu" , "제목2" , "내용2" , new Date (), 0 );
107+
108+ // then
74109 assertThat (firstQuestion .getWriter ()).isEqualTo ("gugu" );
75110 assertThat (firstQuestion .getTitle ()).isEqualTo ("제목1" );
76111 assertThat (firstQuestion .getContents ()).isEqualTo ("내용1" );
@@ -81,50 +116,66 @@ void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception
81116
82117 @ Test
83118 void givenClass_whenGetsPublicFields_thenCorrect () {
119+ // given
84120 final Class <?> questionClass = Question .class ;
85- final Field [] fields = null ;
86121
87- assertThat (fields ).hasSize (0 );
122+ // when
123+ final Field [] fields = questionClass .getFields ();
124+
125+ // then
126+ assertThat (fields ).isEmpty ();
88127 }
89128
90129 @ Test
91130 void givenClass_whenGetsDeclaredFields_thenCorrect () {
131+ // given
92132 final Class <?> questionClass = Question .class ;
93- final Field [] fields = null ;
94133
134+ // when
135+ final Field [] fields = questionClass .getDeclaredFields ();
136+
137+ // then
95138 assertThat (fields ).hasSize (6 );
96139 assertThat (fields [0 ].getName ()).isEqualTo ("questionId" );
97140 }
98141
99142 @ Test
100143 void givenClass_whenGetsFieldsByName_thenCorrect () throws Exception {
144+ // given
101145 final Class <?> questionClass = Question .class ;
102- final Field field = null ;
103146
147+ // when
148+ final Field field = questionClass .getDeclaredField ("questionId" );
149+
150+ // then
104151 assertThat (field .getName ()).isEqualTo ("questionId" );
105152 }
106153
107154 @ Test
108155 void givenClassField_whenGetsType_thenCorrect () throws Exception {
156+ // given
109157 final Field field = Question .class .getDeclaredField ("questionId" );
110- final Class <?> fieldClass = null ;
111158
159+ // when
160+ final Class <?> fieldClass = field .getType ();
161+
162+ // then
112163 assertThat (fieldClass .getSimpleName ()).isEqualTo ("long" );
113164 }
114165
115166 @ Test
116167 void givenClassField_whenSetsAndGetsValue_thenCorrect () throws Exception {
168+ // given
117169 final Class <?> studentClass = Student .class ;
118- final Student student = null ;
119- final Field field = null ;
120-
121- // todo field에 접근 할 수 있도록 만든다.
170+ final Student student = (Student ) studentClass .getDeclaredConstructor ().newInstance ();
171+ final Field field = studentClass .getDeclaredField ("age" );
122172
173+ // when, then
174+ field .setAccessible (true );
123175 assertThat (field .getInt (student )).isZero ();
124176 assertThat (student .getAge ()).isZero ();
125177
126- field .set (null , null );
127-
178+ field .set (student , 99 );
128179 assertThat (field .getInt (student )).isEqualTo (99 );
129180 assertThat (student .getAge ()).isEqualTo (99 );
130181 }
0 commit comments