-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
JUnit5TestEngine.java
103 lines (87 loc) · 4 KB
/
JUnit5TestEngine.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
package org.junit.gen5.engine.junit5;
import org.junit.gen5.api.Test;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.engine.TestEngine;
import org.junit.gen5.engine.TestPlanSpecification;
import org.opentestalliance.TestAbortedException;
import org.opentestalliance.TestSkippedException;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static org.junit.gen5.commons.util.ReflectionUtils.invokeMethod;
import static org.junit.gen5.commons.util.ReflectionUtils.newInstance;
import static org.junit.gen5.engine.TestListenerRegistry.notifyTestExecutionListeners;
public class JUnit5TestEngine implements TestEngine {
// TODO - SBE - could be replace by JUnit5TestEngine.class.getCanonicalName();
private static final String JUNIT5_ENGINE_ID = "junit5";
@Override
public String getId() {
return JUNIT5_ENGINE_ID;
}
@Override
public List<TestDescriptor> discoverTests(TestPlanSpecification specification) {
List<Class<?>> testClasses = fetchTestClasses(specification);
List<TestDescriptor> testDescriptors = testClasses.stream()
.map(Class::getDeclaredMethods)
.flatMap(Arrays::stream)
.filter(method -> method.isAnnotationPresent(Test.class))
.map(method -> new JavaTestDescriptor(getId(), method))
.collect(toList());
testDescriptors.addAll(
specification.getUniqueIds().stream()
.map(JavaTestDescriptor::from)
.collect(toList())
);
return testDescriptors;
}
private List<Class<?>> fetchTestClasses(TestPlanSpecification testPlanSpecification) {
List<Class<?>> testClasses = new LinkedList<>();
// Add specified test classes directly
testClasses.addAll(testPlanSpecification.getClasses());
// Add test classes by name
for (String className : testPlanSpecification.getClassNames()) {
try {
testClasses.add(Class.forName(className));
} catch (ClassNotFoundException e) {
String msg = "Could not find test class '%s' in the classpath!";
throw new IllegalArgumentException(format(msg, className));
}
}
// TODO - SBE - Add classes for packages
// TODO - SBE - Add classes for package names
// TODO - SBE - Add classes for paths
// TODO - SBE - Add classes for file names
return testClasses;
}
@Override
public boolean supports(TestDescriptor testDescriptor) {
return testDescriptor instanceof JavaTestDescriptor;
}
@Override
public void execute(Collection<TestDescriptor> testDescriptors) {
for (TestDescriptor testDescriptor : testDescriptors) {
try {
notifyTestExecutionListeners(testListener -> testListener.testStarted(testDescriptor));
JavaTestDescriptor javaTestDescriptor = (JavaTestDescriptor) testDescriptor;
Object testInstance = newInstance(javaTestDescriptor.getTestClass());
invokeMethod(javaTestDescriptor.getTestMethod(), testInstance);
notifyTestExecutionListeners(testListener -> testListener.testSucceeded(testDescriptor));
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof TestSkippedException) {
notifyTestExecutionListeners(testListener -> testListener.testSkipped(testDescriptor, e.getTargetException()));
} else if (e.getTargetException() instanceof TestAbortedException) {
notifyTestExecutionListeners(testListener -> testListener.testAborted(testDescriptor, e.getTargetException()));
} else {
notifyTestExecutionListeners(testListener -> testListener.testFailed(testDescriptor, e.getTargetException()));
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(
String.format("Test %s not well-formed and cannot be executed! ", testDescriptor.getUniqueId()));
}
}
}
}