1
2
3
4
5
6
7
8
9
10
11
12
13 package org.kuali.rice.test.runners;
14
15 import java.lang.reflect.Method;
16
17 import org.apache.commons.beanutils.MethodUtils;
18 import org.junit.internal.runners.InitializationError;
19 import org.junit.internal.runners.JUnit4ClassRunner;
20 import org.junit.runner.notification.RunNotifier;
21 import org.kuali.rice.test.MethodAware;
22
23
24
25
26
27
28
29
30 public class RiceUnitTestClassRunner extends JUnit4ClassRunner {
31
32 private Method currentMethod;
33
34 public RiceUnitTestClassRunner(final Class<?> testClass) throws InitializationError {
35 super(testClass);
36
37 }
38
39 @Override
40 protected void invokeTestMethod(Method method, RunNotifier runNotifier) {
41 this.currentMethod = method;
42 try {
43
44 super.invokeTestMethod(method, runNotifier);
45 } finally {
46 this.currentMethod = null;
47 }
48 }
49
50 @Override
51 protected Object createTest() throws Exception {
52 Object test = super.createTest();
53 setTestName(test, currentMethod);
54 setTestMethod(test, currentMethod);
55
56 return test;
57 }
58
59
60
61
62
63 protected void setTestMethod(Object test, Method method) {
64 try {
65 if (test instanceof MethodAware) {
66 ((MethodAware) test).setTestMethod(method);
67 }
68 } catch (Exception e) {
69 e.printStackTrace();
70
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83
84 protected void setTestName(final Object test, final Method testMethod) {
85 try {
86 String name = testMethod == null ? "" : testMethod.getName();
87 final Method setNameMethod = MethodUtils.getAccessibleMethod(test.getClass(), "setName", new Class[]{String.class});
88 if (setNameMethod != null) {
89 setNameMethod.invoke(test, new Object[]{name});
90 }
91 } catch (final Exception e) {
92
93 }
94 }
95
96 }