All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.junit.jupiter.engine.descriptor.LifecycleMethodUtils Maven / Gradle / Ivy

There is a newer version: 5.11.3
Show newest version
/*
 * Copyright 2015-2022 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 v2.0 which
 * accompanies this distribution and is available at
 *
 * https://www.eclipse.org/legal/epl-v20.html
 */

package org.junit.jupiter.engine.descriptor;

import static org.junit.platform.commons.util.AnnotationUtils.findAnnotatedMethods;
import static org.junit.platform.commons.util.ReflectionUtils.returnsVoid;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.platform.commons.JUnitException;
import org.junit.platform.commons.util.ReflectionUtils;
import org.junit.platform.commons.util.ReflectionUtils.HierarchyTraversalMode;

/**
 * Collection of utilities for working with test lifecycle methods.
 *
 * @since 5.0
 */
final class LifecycleMethodUtils {

	private LifecycleMethodUtils() {
		/* no-op */
	}

	static List findBeforeAllMethods(Class testClass, boolean requireStatic) {
		return findMethodsAndAssertStaticAndNonPrivate(testClass, requireStatic, BeforeAll.class,
			HierarchyTraversalMode.TOP_DOWN);
	}

	static List findAfterAllMethods(Class testClass, boolean requireStatic) {
		return findMethodsAndAssertStaticAndNonPrivate(testClass, requireStatic, AfterAll.class,
			HierarchyTraversalMode.BOTTOM_UP);
	}

	static List findBeforeEachMethods(Class testClass) {
		return findMethodsAndAssertNonStaticAndNonPrivate(testClass, BeforeEach.class, HierarchyTraversalMode.TOP_DOWN);
	}

	static List findAfterEachMethods(Class testClass) {
		return findMethodsAndAssertNonStaticAndNonPrivate(testClass, AfterEach.class, HierarchyTraversalMode.BOTTOM_UP);
	}

	private static List findMethodsAndAssertStaticAndNonPrivate(Class testClass, boolean requireStatic,
			Class annotationType, HierarchyTraversalMode traversalMode) {

		List methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);
		if (requireStatic) {
			methods.forEach(method -> assertStatic(annotationType, method));
		}
		methods.forEach(method -> assertNonPrivate(annotationType, method));
		return methods;
	}

	private static List findMethodsAndAssertNonStaticAndNonPrivate(Class testClass,
			Class annotationType, HierarchyTraversalMode traversalMode) {

		List methods = findMethodsAndCheckVoidReturnType(testClass, annotationType, traversalMode);
		methods.forEach(method -> {
			assertNonStatic(annotationType, method);
			assertNonPrivate(annotationType, method);
		});
		return methods;
	}

	private static List findMethodsAndCheckVoidReturnType(Class testClass,
			Class annotationType, HierarchyTraversalMode traversalMode) {

		List methods = findAnnotatedMethods(testClass, annotationType, traversalMode);
		methods.forEach(method -> assertVoid(annotationType, method));
		return methods;
	}

	private static void assertStatic(Class annotationType, Method method) {
		if (ReflectionUtils.isNotStatic(method)) {
			throw new JUnitException(String.format(
				"@%s method '%s' must be static unless the test class is annotated with @TestInstance(Lifecycle.PER_CLASS).",
				annotationType.getSimpleName(), method.toGenericString()));
		}
	}

	private static void assertNonStatic(Class annotationType, Method method) {
		if (ReflectionUtils.isStatic(method)) {
			throw new JUnitException(String.format("@%s method '%s' must not be static.",
				annotationType.getSimpleName(), method.toGenericString()));
		}
	}

	private static void assertNonPrivate(Class 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 annotationType, Method method) {
		if (!returnsVoid(method)) {
			throw new JUnitException(String.format("@%s method '%s' must not return a value.",
				annotationType.getSimpleName(), method.toGenericString()));
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy