Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
80 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
junit5-engine/src/main/java/org/junit/gen5/engine/junit5/TestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,58 @@ | |||
|
|||
package org.junit.gen5.engine.junit5; | |||
|
|||
import static java.util.stream.Collectors.*; | |||
import static org.junit.gen5.commons.util.ReflectionUtils.*; | |||
|
|||
import java.lang.annotation.Annotation; | |||
import java.lang.reflect.Method; | |||
import java.util.Arrays; | |||
import java.util.List; | |||
|
|||
import org.junit.gen5.api.After; | |||
import org.junit.gen5.api.Before; | |||
|
|||
/** | |||
* @author Sam Brannen | |||
* @author Matthias Merdes | |||
* @since 5.0 | |||
*/ | |||
class TestExecutor { | |||
|
|||
private final JavaTestDescriptor testDescriptor; | |||
|
|||
|
|||
TestExecutor(JavaTestDescriptor testDescriptor) { | |||
this.testDescriptor = testDescriptor; | |||
} | |||
|
|||
void execute() throws Exception { | |||
Class<?> testClass = this.testDescriptor.getTestClass(); | |||
|
|||
// TODO Extract test instantiation | |||
Object testInstance = newInstance(testClass); | |||
|
|||
executeBeforeMethods(testClass, testInstance); | |||
invokeMethod(this.testDescriptor.getTestMethod(), testInstance); | |||
executeAfterMethods(testClass, testInstance); | |||
} | |||
|
|||
private void executeBeforeMethods(Class<?> testClass, Object testInstance) throws Exception { | |||
for (Method method : findAnnotatedMethods(testClass, Before.class)) { | |||
invokeMethod(method, testInstance); | |||
} | |||
} | |||
|
|||
private void executeAfterMethods(Class<?> testClass, Object testInstance) throws Exception { | |||
for (Method method : findAnnotatedMethods(testClass, After.class)) { | |||
invokeMethod(method, testInstance); | |||
} | |||
} | |||
|
|||
private List<Method> findAnnotatedMethods(Class<?> testClass, Class<? extends Annotation> annotationType) { | |||
return Arrays.stream(testClass.getDeclaredMethods()) | |||
.filter(method -> method.isAnnotationPresent(annotationType)) | |||
.collect(toList()); | |||
} | |||
|
|||
} |