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