@@ -33,4 +33,81 @@ public void initializeStaticVariable_checkAssignedValues() {
3333 }
3434
3535 }
36+
37+ @ Test
38+ public void initializeStaticVariable_checkStaticBlock () {
39+
40+ try {
41+ Class <?> staticVariableDemo = this .getClass ()
42+ .getClassLoader ()
43+ .loadClass ("com.baeldung.staticvariables.StaticVariableDemo" );
44+
45+ Field field1 = staticVariableDemo .getField ("z" );
46+
47+ assertThat (field1 .getInt (staticVariableDemo )).isEqualTo (30 );
48+
49+ Field field2 = staticVariableDemo .getField ("a" );
50+
51+ assertThat (field2 .getInt (staticVariableDemo )).isEqualTo (50 );
52+
53+ } catch (ClassNotFoundException | NoSuchFieldException | SecurityException e ) {
54+ e .printStackTrace ();
55+ } catch (IllegalArgumentException e ) {
56+ e .printStackTrace ();
57+ } catch (IllegalAccessException e ) {
58+ e .printStackTrace ();
59+ }
60+
61+ }
62+
63+ @ Test
64+ public void initializeStaticVariable_checkFinalValues () {
65+
66+ try {
67+ Class <?> staticVariableDemo = this .getClass ()
68+ .getClassLoader ()
69+ .loadClass ("com.baeldung.staticvariables.StaticVariableDemo" );
70+
71+ Field field1 = staticVariableDemo .getField ("b" );
72+
73+ assertThat (field1 .getInt (staticVariableDemo )).isEqualTo (100 );
74+
75+ } catch (ClassNotFoundException | NoSuchFieldException | SecurityException e ) {
76+ e .printStackTrace ();
77+ } catch (IllegalArgumentException e ) {
78+ e .printStackTrace ();
79+ } catch (IllegalAccessException e ) {
80+ e .printStackTrace ();
81+ }
82+
83+ }
84+
85+ @ Test
86+ public void initializeStaticVariable_checkInnerClassValues () {
87+
88+ try {
89+ Class <?> staticVariableDemo = this .getClass ()
90+ .getClassLoader ()
91+ .loadClass ("com.baeldung.staticvariables.StaticVariableDemo" );
92+
93+ Class <?>[] nestedClasses = staticVariableDemo .getClasses ();
94+
95+ for (Class <?> nestedClass : nestedClasses ) {
96+ if (nestedClass .getName ()
97+ .equals ("Nested" )) {
98+
99+ Field field1 = nestedClass .getField ("nestedClassStaticVariable" );
100+ assertThat (field1 .get (nestedClass )).isEqualTo ("test" );
101+ }
102+ }
103+
104+ } catch (ClassNotFoundException | NoSuchFieldException | SecurityException e ) {
105+ e .printStackTrace ();
106+ } catch (IllegalArgumentException e ) {
107+ e .printStackTrace ();
108+ } catch (IllegalAccessException e ) {
109+ e .printStackTrace ();
110+ }
111+
112+ }
36113}
0 commit comments