Debian Codegraph @

junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeEach.java

raw symbols SCIP
1 /*
2 * Copyright 2015-2024 the original author or authors.
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v2.0 which
6 * accompanies this distribution and is available at
7 *
8 * https://www.eclipse.org/legal/epl-v20.html
9 */
10
11 package org.junit.jupiter.api;
12
13 import static org.apiguardian.api.API.Status.STABLE;
14
15 import java.lang.annotation.Documented;
16 import java.lang.annotation.ElementType;
17 import java.lang.annotation.Retention;
18 import java.lang.annotation.RetentionPolicy;
19 import java.lang.annotation.Target;
20
21 import org.apiguardian.api.API;
22
23 /**
24 * {@code @BeforeEach} is used to signal that the annotated method should be
25 * executed <em>before</em> <strong>each</strong> {@code @Test},
26 * {@code @RepeatedTest}, {@code @ParameterizedTest}, {@code @TestFactory},
27 * and {@code @TestTemplate} method in the current test class.
28 *
29 * <h2>Method Signatures</h2>
30 *
31 * <p>{@code @BeforeEach} methods must have a {@code void} return type and must
32 * not be {@code static}. Using {@code private} visibility is strongly
33 * discouraged and will be disallowed in a future release.
34 * They may optionally declare parameters to be resolved by
35 * {@link org.junit.jupiter.api.extension.ParameterResolver ParameterResolvers}.
36 *
37 * <h2>Inheritance and Execution Order</h2>
38 *
39 * <p>{@code @BeforeEach} methods are inherited from superclasses as long as they
40 * are not <em>overridden</em> or <em>superseded</em> (i.e., replaced based on
41 * signature only, irrespective of Java's visibility rules). Furthermore,
42 * {@code @BeforeEach} methods from superclasses will be executed before
43 * {@code @BeforeEach} methods in subclasses.
44 *
45 * <p>Similarly, {@code @BeforeEach} methods declared as <em>interface default
46 * methods</em> are inherited as long as they are not overridden, and
47 * {@code @BeforeEach} default methods will be executed before {@code @BeforeEach}
48 * methods in the class that implements the interface.
49 *
50 * <p>JUnit Jupiter does not guarantee the execution order of multiple
51 * {@code @BeforeEach} methods that are declared within a single test class or
52 * test interface. While it may at times appear that these methods are invoked
53 * in alphabetical order, they are in fact sorted using an algorithm that is
54 * deterministic but intentionally non-obvious.
55 *
56 * <p>In addition, {@code @BeforeEach} methods are in no way linked to
57 * {@code @AfterEach} methods. Consequently, there are no guarantees with regard
58 * to their <em>wrapping</em> behavior. For example, given two
59 * {@code @BeforeEach} methods {@code createA()} and {@code createB()} as well
60 * as two {@code @AfterEach} methods {@code destroyA()} and {@code destroyB()},
61 * the order in which the {@code @BeforeEach} methods are executed (e.g.
62 * {@code createA()} before {@code createB()}) does not imply any order for the
63 * seemingly corresponding {@code @AfterEach} methods. In other words,
64 * {@code destroyA()} might be called before <em>or</em> after
65 * {@code destroyB()}. The JUnit Team therefore recommends that developers
66 * declare at most one {@code @BeforeEach} method and at most one
67 * {@code @AfterEach} method per test class or test interface unless there are
68 * no dependencies between the {@code @BeforeEach} methods or between the
69 * {@code @AfterEach} methods.
70 *
71 * <h2>Composition</h2>
72 *
73 * <p>{@code @BeforeEach} may be used as a meta-annotation in order to create
74 * a custom <em>composed annotation</em> that inherits the semantics of
75 * {@code @BeforeEach}.
76 *
77 * @since 5.0
78 * @see AfterEach
79 * @see BeforeAll
80 * @see AfterAll
81 * @see Test
82 * @see RepeatedTest
83 * @see TestFactory
84 * @see TestTemplate
85 */
86 @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
87 @Retention(RetentionPolicy.RUNTIME)
88 @Documented
89 @API(status = STABLE, since = "5.0")
90 public @interface BeforeEach {
91 }