/
JUnit5MethodDescriptor.java
84 lines (72 loc) · 2.25 KB
/
JUnit5MethodDescriptor.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
/*
* Copyright 2015 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.gen5.engine.junit5;
import static org.junit.gen5.commons.util.AnnotationUtils.findAnnotatedMethods;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import org.junit.gen5.api.AfterEach;
import org.junit.gen5.api.BeforeEach;
import org.junit.gen5.commons.util.ReflectionUtils;
import org.junit.gen5.commons.util.ReflectionUtils.MethodSortOrder;
import org.junit.gen5.engine.AbstractTestDescriptor;
import org.junit.gen5.engine.Child;
public class JUnit5MethodDescriptor extends AbstractTestDescriptor implements Child<JUnit5Context> {
private final Class<?> testClass;
private final Method method;
public JUnit5MethodDescriptor(String parentId, Class<?> testClass, Method method) {
super(parentId + "#" + method.getName());
this.testClass = testClass;
this.method = method;
}
@Override
public String getDisplayName() {
return method.getName();
}
@Override
public boolean isTest() {
return true;
}
@Override
public boolean isContainer() {
return false;
}
@Override
public JUnit5Context execute(JUnit5Context context) throws Throwable {
TestInstanceProvider provider = context.getTestInstanceProvider();
Object testInstance = provider.getTestInstance();
for (Method method : findAnnotatedMethods(testClass, BeforeEach.class, MethodSortOrder.HierarchyDown)) {
ReflectionUtils.invokeMethod(method, testInstance);
}
List<Throwable> throwables = new LinkedList<>();
try {
ReflectionUtils.invokeMethod(method, testInstance);
}
catch (Throwable t) {
throwables.add(t);
}
finally {
for (Method method : findAnnotatedMethods(testClass, AfterEach.class, MethodSortOrder.HierarchyUp)) {
try {
ReflectionUtils.invokeMethod(method, testInstance);
}
catch (Throwable t) {
throwables.add(t);
}
}
}
if (throwables.isEmpty()) {
return context;
}
Throwable t = throwables.get(0);
throwables.stream().skip(1).forEach(t::addSuppressed);
throw t;
}
}