-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Eagerly load and validate @beforeeach & @AfterEach methods
With this commit @beforeeach and @AfterEach methods are looked up and validated eagerly during the discovery phase instead of late in the game during the execution phase. This commit also extracts a new LifecycleMethodUtils class from the ClassTestDescriptor. Issue: #232
- Loading branch information
Showing
2 changed files
with
97 additions
and
66 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
76 changes: 76 additions & 0 deletions
76
...t5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/LifecycleMethodUtils.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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright 2015-2016 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.descriptor; | ||
|
||
import static org.junit.gen5.commons.util.AnnotationUtils.findAnnotatedMethods; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
import org.junit.gen5.api.AfterAll; | ||
import org.junit.gen5.api.AfterEach; | ||
import org.junit.gen5.api.BeforeAll; | ||
import org.junit.gen5.api.BeforeEach; | ||
import org.junit.gen5.commons.JUnitException; | ||
import org.junit.gen5.commons.util.ReflectionUtils; | ||
import org.junit.gen5.commons.util.ReflectionUtils.MethodSortOrder; | ||
|
||
/** | ||
* Collection of utilities for working with test lifecycle methods. | ||
* | ||
* @since 5.0 | ||
*/ | ||
final class LifecycleMethodUtils { | ||
|
||
private LifecycleMethodUtils() { | ||
/* no-op */ | ||
} | ||
|
||
static List<Method> findBeforeAllMethods(Class<?> testClass) { | ||
List<Method> methods = findAnnotatedMethods(testClass, BeforeAll.class, MethodSortOrder.HierarchyDown); | ||
methods.forEach(method -> assertStatic(BeforeAll.class, method)); | ||
return methods; | ||
} | ||
|
||
static List<Method> findAfterAllMethods(Class<?> testClass) { | ||
List<Method> methods = findAnnotatedMethods(testClass, AfterAll.class, MethodSortOrder.HierarchyUp); | ||
methods.forEach(method -> assertStatic(AfterAll.class, method)); | ||
return methods; | ||
} | ||
|
||
static List<Method> findBeforeEachMethods(Class<?> testClass) { | ||
List<Method> methods = findAnnotatedMethods(testClass, BeforeEach.class, MethodSortOrder.HierarchyDown); | ||
methods.forEach(method -> assertNonStatic(BeforeEach.class, method)); | ||
return methods; | ||
} | ||
|
||
static List<Method> findAfterEachMethods(Class<?> testClass) { | ||
List<Method> methods = findAnnotatedMethods(testClass, AfterEach.class, MethodSortOrder.HierarchyUp); | ||
methods.forEach(method -> assertNonStatic(AfterEach.class, method)); | ||
return methods; | ||
} | ||
|
||
private static void assertStatic(Class<? extends Annotation> annotationType, Method method) { | ||
if (!ReflectionUtils.isStatic(method)) { | ||
throw new JUnitException(String.format("@%s method '%s' must be static.", annotationType.getSimpleName(), | ||
method.toGenericString())); | ||
} | ||
} | ||
|
||
private static void assertNonStatic(Class<? extends Annotation> annotationType, Method method) { | ||
if (ReflectionUtils.isStatic(method)) { | ||
throw new JUnitException(String.format("@%s method '%s' must not be static.", | ||
annotationType.getSimpleName(), method.toGenericString())); | ||
} | ||
} | ||
|
||
} |