19
19
import java .util .Deque ;
20
20
import java .util .Stack ;
21
21
22
- import org .junit .Test ;
23
- import org .junit .jupiter .api .Assertions ;
24
- import org .junit .runner .RunWith ;
25
- import org .junit .runners .BlockJUnit4ClassRunner ;
26
- import org .junit .runners .ParentRunner ;
27
- import sun .reflect .Reflection ;
22
+ import org .junit .jupiter .api .Test ;
23
+ import org .junit .jupiter .api .condition .EnabledOnJre ;
24
+ import org .junit .jupiter .api .condition .JRE ;
25
+ import org .junit .jupiter .engine .descriptor .TestMethodTestDescriptor ;
26
+ import org .junit .platform .engine .support .hierarchical .ThrowableCollector ;
28
27
29
- import static org .junit .Assert .assertEquals ;
30
- import static org .junit .Assert .assertNotNull ;
31
- import static org .junit .Assert .assertSame ;
28
+ import static org .junit .jupiter . api . Assertions .assertEquals ;
29
+ import static org .junit .jupiter . api . Assertions .assertNotNull ;
30
+ import static org .junit .jupiter . api . Assertions .assertSame ;
32
31
33
32
/**
34
- * Tests {@link StackLocatorUtilTest }.
33
+ * Tests {@link StackLocatorUtil }.
35
34
*/
36
- @ RunWith (BlockJUnit4ClassRunner .class )
37
35
public class StackLocatorUtilTest {
38
36
39
37
@ Test
38
+ @ EnabledOnJre (JRE .JAVA_8 )
40
39
public void testStackTraceEquivalence () throws Exception {
41
- for (int i = 1 ; i < 15 ; i ++) {
42
- final Class <?> expected = Reflection .getCallerClass (i + StackLocator .JDK_7U25_OFFSET );
40
+ // Frame 8 is a hidden frame and does not show in the stacktrace
41
+ for (int i = 1 ; i < 8 ; i ++) {
42
+ final Class <?> expected = (Class <?>) Class .forName ("sun.reflect.Reflection" )
43
+ .getMethod ("getCallerClass" , int .class )
44
+ .invoke (null , i + StackLocator .JDK_7U25_OFFSET );
43
45
final Class <?> actual = StackLocatorUtil .getCallerClass (i );
44
46
final Class <?> fallbackActual = Class .forName (
45
47
StackLocatorUtil .getStackTraceElement (i ).getClassName ());
@@ -88,22 +90,23 @@ public void testTopElementInStackTrace() {
88
90
final Deque <Class <?>> classes = stackLocator .getCurrentStackTrace ();
89
91
//Removing private class in "PrivateSecurityManagerStackTraceUtil"
90
92
classes .removeFirst ();
91
- Assertions . assertSame (PrivateSecurityManagerStackTraceUtil .class , classes .getFirst ());
93
+ assertSame (PrivateSecurityManagerStackTraceUtil .class , classes .getFirst ());
92
94
}
93
95
94
96
@ Test
95
97
public void testGetCallerClassViaName () throws Exception {
96
- final Class <?> expected = BlockJUnit4ClassRunner .class ;
97
- final Class <?> actual = StackLocatorUtil .getCallerClass ("org.junit.runners.ParentRunner" );
98
+ if (true ) new RuntimeException ().printStackTrace ();
99
+ final Class <?> expected = TestMethodTestDescriptor .class ;
100
+ final Class <?> actual = StackLocatorUtil .getCallerClass ("org.junit.platform.engine.support.hierarchical.ThrowableCollector" );
98
101
// if this test fails in the future, it's probably because of a JUnit upgrade; check the new stack trace and
99
102
// update this test accordingly
100
103
assertSame (expected , actual );
101
104
}
102
105
103
106
@ Test
104
107
public void testGetCallerClassViaAnchorClass () throws Exception {
105
- final Class <?> expected = BlockJUnit4ClassRunner .class ;
106
- final Class <?> actual = StackLocatorUtil .getCallerClass (ParentRunner .class );
108
+ final Class <?> expected = TestMethodTestDescriptor .class ;
109
+ final Class <?> actual = StackLocatorUtil .getCallerClass (ThrowableCollector .class );
107
110
// if this test fails in the future, it's probably because of a JUnit upgrade; check the new stack trace and
108
111
// update this test accordingly
109
112
assertSame (expected , actual );
@@ -113,8 +116,8 @@ public void testGetCallerClassViaAnchorClass() throws Exception {
113
116
public void testLocateClass () {
114
117
final ClassLocator locator = new ClassLocator ();
115
118
final Class <?> clazz = locator .locateClass ();
116
- assertNotNull ("Could not locate class" , clazz );
117
- assertEquals ("Incorrect class" , this .getClass (), clazz );
119
+ assertNotNull (clazz , "Could note locate class" );
120
+ assertEquals (this .getClass (), clazz , "Incorrect class" );
118
121
}
119
122
120
123
}
0 commit comments