-
Notifications
You must be signed in to change notification settings - Fork 1k
/
JUnitMethodFinder.java
129 lines (109 loc) · 3.98 KB
/
JUnitMethodFinder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.testng.junit;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.testng.ITestMethodFinder;
import org.testng.ITestNGMethod;
import org.testng.ITestObjectFactory;
import org.testng.collections.Lists;
import org.testng.internal.ConstructorOrMethod;
import org.testng.internal.TestNGMethod;
import org.testng.internal.annotations.IAnnotationFinder;
import org.testng.internal.reflect.ReflectionHelper;
import org.testng.xml.XmlTest;
/**
* This class locates all test and configuration methods according to JUnit. It is used to change
* the strategy used by TestRunner to locate its test methods.
*
* @deprecated - Support for running JUnit tests stands deprecated as of TestNG <code>7.6.2</code>
*/
@Deprecated
public class JUnitMethodFinder implements ITestMethodFinder {
private final ITestObjectFactory objectFactory;
private final IAnnotationFinder m_annotationFinder;
public JUnitMethodFinder(ITestObjectFactory objectFactory, IAnnotationFinder finder) {
this.objectFactory = objectFactory;
m_annotationFinder = finder;
}
@Override
public ITestNGMethod[] getTestMethods(Class<?> cls, XmlTest xmlTest) {
return privateFindTestMethods(
method -> method.getName().startsWith("test") && method.getParameterTypes().length == 0,
cls);
}
private ITestNGMethod[] privateFindTestMethods(INameFilter filter, Class<?> cls) {
List<ITestNGMethod> vResult = Lists.newArrayList();
// We do not want to walk up the class hierarchy and accept the
// same method twice (e.g. setUp) which would lead to double-invocation.
// All relevant JUnit methods are parameter-less so we store accepted
// method names in a Set to filter out duplicates.
Set<String> acceptedMethodNames = new HashSet<>();
//
// Collect all methods that start with test
//
Class current = cls;
while (!(current == Object.class)) {
Method[] allMethods = ReflectionHelper.excludingMain(current);
for (Method allMethod : allMethods) {
ITestNGMethod m =
new TestNGMethod(objectFactory, allMethod, m_annotationFinder, null, null);
ConstructorOrMethod method = m.getConstructorOrMethod();
String methodName = method.getName();
if (filter.accept(method) && !acceptedMethodNames.contains(methodName)) {
vResult.add(m);
acceptedMethodNames.add(methodName);
}
}
current = current.getSuperclass();
}
return vResult.toArray(new ITestNGMethod[0]);
}
@Override
public ITestNGMethod[] getBeforeTestMethods(Class<?> cls) {
return privateFindTestMethods(method -> "setUp".equals(method.getName()), cls);
}
@Override
public ITestNGMethod[] getAfterTestMethods(Class<?> cls) {
return privateFindTestMethods(method -> "tearDown".equals(method.getName()), cls);
}
@Override
public ITestNGMethod[] getAfterClassMethods(Class<?> cls) {
return new ITestNGMethod[0];
}
@Override
public ITestNGMethod[] getBeforeClassMethods(Class<?> cls) {
return new ITestNGMethod[0];
}
@Override
public ITestNGMethod[] getBeforeSuiteMethods(Class<?> cls) {
return new ITestNGMethod[0];
}
@Override
public ITestNGMethod[] getAfterSuiteMethods(Class<?> cls) {
return new ITestNGMethod[0];
}
@Override
public ITestNGMethod[] getBeforeTestConfigurationMethods(Class<?> testClass) {
return new ITestNGMethod[0];
}
@Override
public ITestNGMethod[] getAfterTestConfigurationMethods(Class<?> testClass) {
return new ITestNGMethod[0];
}
@Override
public ITestNGMethod[] getBeforeGroupsConfigurationMethods(Class<?> testClass) {
return new ITestNGMethod[0];
}
@Override
public ITestNGMethod[] getAfterGroupsConfigurationMethods(Class<?> testClass) {
return new ITestNGMethod[0];
}
}
/**
* @deprecated - Support for running JUnit tests stands deprecated as of TestNG <code>7.6.2</code>
*/
@Deprecated
interface INameFilter {
boolean accept(ConstructorOrMethod method);
}