1+ package com .baeldung .reflection ;
2+
3+ import org .junit .Test ;
4+
5+ import java .lang .reflect .Field ;
6+ import java .util .ArrayList ;
7+ import java .util .Arrays ;
8+ import java .util .Collections ;
9+ import java .util .List ;
10+
11+ import static org .junit .Assert .*;
12+
13+ public class PersonAndEmployeeReflectionTest {
14+
15+ // Fields names
16+ private static final String LAST_NAME_FIELD = "lastName" ;
17+ private static final String FIRST_NAME_FIELD = "firstName" ;
18+ private static final String EMPLOYEE_ID_FIELD = "employeeId" ;
19+ private static final String MONTH_EMPLOYEE_REWARD_FIELD = "reward" ;
20+
21+ @ Test
22+ public void givenPersonClass_whenGetDeclaredFields_thenTwoFields () {
23+ // When
24+ Field [] allFields = Person .class .getDeclaredFields ();
25+
26+ // Then
27+ assertEquals (2 , allFields .length );
28+
29+ Field lastNameField = allFields [0 ];
30+ assertEquals (LAST_NAME_FIELD , lastNameField .getName ());
31+ assertEquals (String .class , lastNameField .getType ());
32+
33+ Field firstNameField = allFields [1 ];
34+ assertEquals (FIRST_NAME_FIELD , firstNameField .getName ());
35+ assertEquals (String .class , firstNameField .getType ());
36+ }
37+
38+ @ Test
39+ public void givenEmployeeClass_whenSuperClassGetDeclaredFields_thenOneField () {
40+ // When
41+ Field [] allFields = Employee .class .getSuperclass ().getDeclaredFields ();
42+
43+ // Then
44+ assertEquals (2 , allFields .length );
45+
46+ Field lastNameField = allFields [0 ];
47+ assertEquals (LAST_NAME_FIELD , lastNameField .getName ());
48+ assertEquals (String .class , lastNameField .getType ());
49+
50+ Field firstNameField = allFields [1 ];
51+ assertEquals (FIRST_NAME_FIELD , firstNameField .getName ());
52+ assertEquals (String .class , firstNameField .getType ());
53+ }
54+
55+ @ Test
56+ public void givenEmployeeClass_whenGetDeclaredFields_thenOneField () {
57+ // When
58+ Field [] allFields = Employee .class .getDeclaredFields ();
59+
60+ // Then
61+ assertEquals (1 , allFields .length );
62+
63+ Field employeeIdField = allFields [0 ];
64+ assertEquals (EMPLOYEE_ID_FIELD , employeeIdField .getName ());
65+ assertEquals (int .class , employeeIdField .getType ());
66+ }
67+
68+ @ Test
69+ public void givenEmployeeClass_whenGetDeclaredFieldsOnBothClasses_thenThreeFields () {
70+ // When
71+ Field [] personFields = Employee .class .getSuperclass ().getDeclaredFields ();
72+ Field [] employeeFields = Employee .class .getDeclaredFields ();
73+ Field [] allFields = new Field [employeeFields .length + personFields .length ];
74+ Arrays .setAll (allFields , i -> (i < personFields .length ? personFields [i ] : employeeFields [i - personFields .length ]));
75+
76+ // Then
77+ assertEquals (3 , allFields .length );
78+
79+ Field lastNameField = allFields [0 ];
80+ assertEquals (LAST_NAME_FIELD , lastNameField .getName ());
81+ assertEquals (String .class , lastNameField .getType ());
82+
83+ Field firstNameField = allFields [1 ];
84+ assertEquals (FIRST_NAME_FIELD , firstNameField .getName ());
85+ assertEquals (String .class , firstNameField .getType ());
86+
87+ Field employeeIdField = allFields [2 ];
88+ assertEquals (EMPLOYEE_ID_FIELD , employeeIdField .getName ());
89+ assertEquals (int .class , employeeIdField .getType ());
90+ }
91+
92+ @ Test
93+ public void givenMonthEmployeeClass_whenGetAllFields_thenFourFields () {
94+ // When
95+ List <Field > allFields = getAllFields (MonthEmployee .class );
96+
97+ // Then
98+ assertEquals (4 , allFields .size ());
99+
100+ Field lastNameField = allFields .get (0 );
101+ assertEquals (LAST_NAME_FIELD , lastNameField .getName ());
102+ assertEquals (String .class , lastNameField .getType ());
103+
104+ Field firstNameField = allFields .get (1 );
105+ assertEquals (FIRST_NAME_FIELD , firstNameField .getName ());
106+ assertEquals (String .class , firstNameField .getType ());
107+
108+ Field employeeIdField = allFields .get (2 );
109+ assertEquals (EMPLOYEE_ID_FIELD , employeeIdField .getName ());
110+ assertEquals (int .class , employeeIdField .getType ());
111+
112+ Field monthEmployeeRewardField = allFields .get (3 );
113+ assertEquals (MONTH_EMPLOYEE_REWARD_FIELD , monthEmployeeRewardField .getName ());
114+ assertEquals (double .class , monthEmployeeRewardField .getType ());
115+ }
116+
117+ public List <Field > getAllFields (Class clazz ) {
118+ if (clazz == null ) {
119+ return Collections .emptyList ();
120+ }
121+
122+ List <Field > result = new ArrayList <>();
123+ result .addAll (getAllFields (clazz .getSuperclass ()));
124+ result .addAll (Arrays .asList (clazz .getDeclaredFields ()));
125+ return result ;
126+ }
127+
128+ }
0 commit comments