Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ static List<Method> findAfterAllMethods(Class<?> testClass, boolean requireStati
}

static List<Method> findBeforeEachMethods(Class<?> testClass) {
return findMethodsAndAssertNonStatic(testClass, BeforeEach.class, HierarchyTraversalMode.TOP_DOWN);
return findMethodsAndAssertNonStaticAndNonPrivate(testClass, BeforeEach.class, HierarchyTraversalMode.TOP_DOWN);
}

static List<Method> findAfterEachMethods(Class<?> testClass) {
return findMethodsAndAssertNonStatic(testClass, AfterEach.class, HierarchyTraversalMode.BOTTOM_UP);
return findMethodsAndAssertNonStaticAndNonPrivate(testClass, AfterEach.class, HierarchyTraversalMode.BOTTOM_UP);
}

private static void assertStatic(Class<? extends Annotation> annotationType, Method method) {
Expand All @@ -67,6 +67,13 @@ private static void assertNonStatic(Class<? extends Annotation> annotationType,
}
}

private static void assertNonPrivate(Class<? extends Annotation> annotationType, Method method) {
if (ReflectionUtils.isPrivate(method)) {
throw new JUnitException(String.format("@%s method '%s' must not be private.",
annotationType.getSimpleName(), method.toGenericString()));
}
}

private static void assertVoid(Class<? extends Annotation> annotationType, Method method) {
if (!returnsVoid(method)) {
throw new JUnitException(String.format("@%s method '%s' must not return a value.",
Expand All @@ -83,10 +90,14 @@ private static List<Method> findMethodsAndAssertStatic(Class<?> testClass, boole
return methods;
}

private static List<Method> findMethodsAndAssertNonStatic(Class<?> testClass,
private static List<Method> findMethodsAndAssertNonStaticAndNonPrivate(Class<?> testClass,
Class<? extends Annotation> annotationType, HierarchyTraversalMode traversalMode) {
List<Method> methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);
methods.forEach(method -> assertNonStatic(annotationType, method));
methods.forEach(method -> {
assertNonStatic(annotationType, method);
assertNonPrivate(annotationType, method);
});

return methods;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.junit.platform.testkit.engine.Events;

/**
* Integration tests that very proper handling of invalid configuration for
* Integration tests that verify proper handling of invalid configuration for
* lifecycle methods in conjunction with the {@link JupiterTestEngine}.
*
* <p>In general, configuration errors should not be thrown until the
Expand All @@ -44,13 +44,23 @@ void executeValidTestCaseAlongsideTestCaseWithInvalidAfterAllDeclaration() {
}

@Test
void executeValidTestCaseAlongsideTestCaseWithInvalidBeforeEachDeclaration() {
assertExecutionResults(TestCaseWithInvalidBeforeEachMethod.class);
void executeValidTestCaseAlongsideTestCaseWithInvalidStaticBeforeEachDeclaration() {
assertExecutionResults(TestCaseWithInvalidStaticBeforeEachMethod.class);
}

@Test
void executeValidTestCaseAlongsideTestCaseWithInvalidAfterEachDeclaration() {
assertExecutionResults(TestCaseWithInvalidAfterEachMethod.class);
void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateBeforeEachDeclaration() {
assertExecutionResults(TestCaseWithInvalidPrivateBeforeEachMethod.class);
}

@Test
void executeValidTestCaseAlongsideTestCaseWithInvalidStaticAfterEachDeclaration() {
assertExecutionResults(TestCaseWithInvalidStaticAfterEachMethod.class);
}

@Test
void executeValidTestCaseAlongsideTestCaseWithInvalidPrivateAfterEachDeclaration() {
assertExecutionResults(TestCaseWithInvalidPrivateAfterEachMethod.class);
}

private void assertExecutionResults(Class<?> invalidTestClass) {
Expand Down Expand Up @@ -104,7 +114,7 @@ void test() {
}
}

static class TestCaseWithInvalidBeforeEachMethod {
static class TestCaseWithInvalidStaticBeforeEachMethod {

// must NOT be static
@BeforeEach
Expand All @@ -116,7 +126,19 @@ void test() {
}
}

static class TestCaseWithInvalidAfterEachMethod {
static class TestCaseWithInvalidPrivateBeforeEachMethod {

// must NOT be private
@BeforeEach
private void beforeEach() {
}

@Test
void test() {
}
}

static class TestCaseWithInvalidStaticAfterEachMethod {

// must NOT be static
@AfterEach
Expand All @@ -128,4 +150,16 @@ void test() {
}
}

static class TestCaseWithInvalidPrivateAfterEachMethod {

// must NOT be private
@AfterEach
private void afterEach() {
}

@Test
void test() {
}
}

}