-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSingleMethodRunner.java
More file actions
73 lines (54 loc) · 1.89 KB
/
Copy pathSingleMethodRunner.java
File metadata and controls
73 lines (54 loc) · 1.89 KB
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
package se.ericthelin.junit.singlemethodrunner;
import java.util.Collections;
import java.util.List;
import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.manipulation.Filter;
import org.junit.runner.manipulation.NoTestsRemainException;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.ParentRunner;
import org.junit.runners.model.InitializationError;
public class SingleMethodRunner extends ParentRunner<Runner> {
private static final String TEST_METHOD_PROPERTY_NAME = "test.method";
private final List<Runner> children;
public SingleMethodRunner(Class<?> klass) throws InitializationError {
super(klass);
try {
this.children = computeChildren();
} catch (RuntimeException | ClassNotFoundException
| NoTestsRemainException e) {
throw new InitializationError(e);
}
}
private List<Runner> computeChildren() throws InitializationError,
ClassNotFoundException, NoTestsRemainException {
String testMethodPattern = System
.getProperty(TEST_METHOD_PROPERTY_NAME);
if (testMethodPattern == null) {
return Collections.emptyList();
}
return Collections
.singletonList(junitRunnerFor(new TestMethodReference(
testMethodPattern)));
}
private static Runner junitRunnerFor(TestMethodReference reference)
throws InitializationError, NoTestsRemainException {
BlockJUnit4ClassRunner result = new BlockJUnit4ClassRunner(
reference.getTestClass());
result.filter(Filter.matchMethodDescription(reference.toDescription()));
return result;
}
@Override
protected List<Runner> getChildren() {
return children;
}
@Override
protected Description describeChild(Runner child) {
return child.getDescription();
}
@Override
protected void runChild(Runner child, RunNotifier notifier) {
child.run(notifier);
}
}