1+ package com .baeldung .synthetic ;
2+
3+ import java .lang .reflect .Constructor ;
4+ import java .lang .reflect .Field ;
5+ import java .lang .reflect .Method ;
6+
7+ import org .junit .Assert ;
8+ import org .junit .Test ;
9+
10+ /**
11+ * Unit test for {@link SyntheticFieldDemo}, {@link SyntheticMethodDemo},
12+ * {@link SyntheticConstructorDemo} and {@link BridgeMethodDemo} classes.
13+ *
14+ * @author Donato Rimenti
15+ *
16+ */
17+ public class SyntheticUnitTest {
18+
19+ /**
20+ * Tests that the {@link SyntheticMethodDemo.NestedClass} contains two synthetic
21+ * methods.
22+ */
23+ @ Test
24+ public void givenSyntheticMethod_whenIsSinthetic_thenTrue () {
25+ // Checks that the nested class contains exactly two synthetic methods.
26+ Method [] methods = SyntheticMethodDemo .NestedClass .class .getDeclaredMethods ();
27+ Assert .assertEquals ("This class should contain only two methods" , 2 , methods .length );
28+
29+ for (Method m : methods ) {
30+ System .out .println ("Method: " + m .getName () + ", isSynthetic: " + m .isSynthetic ());
31+ Assert .assertTrue ("All the methods of this class should be synthetic" , m .isSynthetic ());
32+ }
33+ }
34+
35+ /**
36+ * Tests that {@link SyntheticConstructorDemo.NestedClass} contains a synthetic
37+ * constructor.
38+ */
39+ @ Test
40+ public void givenSyntheticConstructor_whenIsSinthetic_thenTrue () {
41+ // Checks that the nested class contains exactly a synthetic
42+ // constructor.
43+ int syntheticConstructors = 0 ;
44+ Constructor <?>[] constructors = SyntheticConstructorDemo .NestedClass .class .getDeclaredConstructors ();
45+ Assert .assertEquals ("This class should contain only two constructors" , 2 , constructors .length );
46+
47+ for (Constructor <?> c : constructors ) {
48+ System .out .println ("Constructor: " + c .getName () + ", isSynthetic: " + c .isSynthetic ());
49+
50+ // Counts the synthetic constructors.
51+ if (c .isSynthetic ()) {
52+ syntheticConstructors ++;
53+ }
54+ }
55+
56+ // Checks that there's exactly one synthetic constructor.
57+ Assert .assertEquals (1 , syntheticConstructors );
58+ }
59+
60+ /**
61+ * Tests that {@link SyntheticFieldDemo.NestedClass} contains a synthetic field.
62+ */
63+ @ Test
64+ public void givenSyntheticField_whenIsSinthetic_thenTrue () {
65+ // This class should contain exactly one synthetic field.
66+ Field [] fields = SyntheticFieldDemo .NestedClass .class .getDeclaredFields ();
67+ Assert .assertEquals ("This class should contain only one field" , 1 , fields .length );
68+
69+ for (Field f : fields ) {
70+ System .out .println ("Field: " + f .getName () + ", isSynthetic: " + f .isSynthetic ());
71+ Assert .assertTrue ("All the fields of this class should be synthetic" , f .isSynthetic ());
72+ }
73+ }
74+
75+ /**
76+ * Tests that {@link BridgeMethodDemo} contains a synthetic bridge method.
77+ */
78+ @ Test
79+ public void givenBridgeMethod_whenIsBridge_thenTrue () {
80+ // This class should contain exactly one synthetic bridge method.
81+ int syntheticMethods = 0 ;
82+ Method [] methods = BridgeMethodDemo .class .getDeclaredMethods ();
83+ for (Method m : methods ) {
84+ System .out .println (
85+ "Method: " + m .getName () + ", isSynthetic: " + m .isSynthetic () + ", isBridge: " + m .isBridge ());
86+
87+ // Counts the synthetic methods and checks that they are also bridge
88+ // methods.
89+ if (m .isSynthetic ()) {
90+ syntheticMethods ++;
91+ Assert .assertTrue ("The synthetic method in this class should also be a bridge method" , m .isBridge ());
92+ }
93+ }
94+
95+ // Checks that there's exactly one synthetic bridge method.
96+ Assert .assertEquals ("There should be exactly 1 synthetic bridge method in this class" , 1 , syntheticMethods );
97+ }
98+
99+ }
0 commit comments