4
4
5
5
import org .junit .jupiter .api .Test ;
6
6
7
+ import java .time .Month ;
8
+
7
9
class GetClassExampleTest {
10
+
11
+ @ Test
12
+ void testVoid () {
13
+ Class <Void > c = void .class ;
14
+ assertEquals ("void" , c .getName ());
15
+ assertEquals ("void" , c .getSimpleName ());
16
+ assertEquals ("java.lang" , c .getPackageName ());
17
+ assertEquals ("void" , c .toString ());
18
+ }
19
+
8
20
@ Test
9
21
void testGetClass () throws ClassNotFoundException {
10
- Class cls = String .class ;
11
- Class cls2 = "hello" .getClass ();
12
- Class cls3 = Class .forName ("java.lang.String" );
13
- assertTrue (cls == cls2 && cls == cls3 );
22
+ Class <String > cls = String .class ;
23
+ Class <?> cls2 = "hello" .getClass ();
24
+ Class <?> cls3 = Class .forName ("java.lang.String" );
25
+ assertSame (cls , cls2 );
26
+ assertSame (cls , cls3 );
27
+ }
28
+
29
+ @ Test
30
+ void testGetClassByTYPE () {
31
+ Class <?> c = Integer .TYPE ;
32
+ assertEquals ("int" , c .getName ());
33
+ assertEquals ("int" , c .getSimpleName ());
34
+ assertEquals ("java.lang" , c .getPackageName ());
35
+ assertEquals ("int" , c .toString ());
36
+ assertTrue (c .isPrimitive ());
37
+ }
38
+
39
+ @ Test
40
+ void testGetClassByClassloader () throws ClassNotFoundException {
41
+ ClassLoader classLoader = GetClassExample .class .getClassLoader ();
42
+ Class <?> cls = classLoader .loadClass ("java.lang.String" );
43
+ assertEquals ("java.lang.String" , cls .getName ());
14
44
}
15
45
16
46
@ Test
17
47
void testStringClass () {
18
- Class cls = "" .getClass ();
48
+ Class <?> cls = "" .getClass ();
19
49
assertEquals ("java.lang.String" , cls .getName ());
20
50
assertEquals ("String" , cls .getSimpleName ());
21
51
assertEquals ("java.lang" , cls .getPackageName ());
@@ -27,7 +57,7 @@ void testStringClass() {
27
57
28
58
@ Test
29
59
void testInterface () {
30
- Class cls = Runnable .class ;
60
+ Class < Runnable > cls = Runnable .class ;
31
61
assertEquals ("java.lang.Runnable" , cls .getName ());
32
62
assertEquals ("Runnable" , cls .getSimpleName ());
33
63
assertEquals ("java.lang" , cls .getPackageName ());
@@ -39,7 +69,7 @@ void testInterface() {
39
69
40
70
@ Test
41
71
void testMonth () {
42
- Class cls = java .time .Month .class ;
72
+ Class < Month > cls = java .time .Month .class ;
43
73
assertEquals ("java.time.Month" , cls .getName ());
44
74
assertEquals ("Month" , cls .getSimpleName ());
45
75
assertEquals ("java.time" , cls .getPackageName ());
@@ -51,7 +81,7 @@ void testMonth() {
51
81
52
82
@ Test
53
83
void testStringArray () {
54
- Class cls = String [].class ;
84
+ Class < String []> cls = String [].class ;
55
85
assertEquals ("[Ljava.lang.String;" , cls .getName ());
56
86
assertEquals ("String[]" , cls .getSimpleName ());
57
87
assertEquals ("java.lang" , cls .getPackageName ());
@@ -63,7 +93,7 @@ void testStringArray() {
63
93
64
94
@ Test
65
95
void testPrimitive () {
66
- Class cls = int .class ;
96
+ Class <?> cls = int .class ;
67
97
assertEquals ("int" , cls .getName ());
68
98
assertEquals ("int" , cls .getSimpleName ());
69
99
assertEquals ("java.lang" , cls .getPackageName ());
0 commit comments