| 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.Inherited; |
| 18 | import java.lang.annotation.Retention; |
| 19 | import java.lang.annotation.RetentionPolicy; |
| 20 | import java.lang.annotation.Target; |
| 21 | |
| 22 | import org.apiguardian.api.API; |
| 23 | |
| 24 | /** |
| 25 | * {@code @TestInstance} is a type-level annotation that is used to configure |
| 26 | * the {@linkplain Lifecycle lifecycle} of test instances for the annotated |
| 27 | * test class or test interface. |
| 28 | * |
| 29 | * <p>If {@code @TestInstance} is not explicitly declared on a test class or |
| 30 | * on a test interface implemented by a test class, the lifecycle mode will |
| 31 | * implicitly default to {@link Lifecycle#PER_METHOD PER_METHOD}. Note, however, |
| 32 | * that an explicit lifecycle mode is <em>inherited</em> within a test class |
| 33 | * hierarchy. In addition, the <em>default</em> lifecycle mode may be overridden |
| 34 | * via the {@value Lifecycle#DEFAULT_LIFECYCLE_PROPERTY_NAME} <em>configuration |
| 35 | * parameter</em> which can be supplied via the {@code Launcher} API, build tools |
| 36 | * (e.g., Gradle and Maven), a JVM system property, or the JUnit Platform |
| 37 | * configuration file (i.e., a file named {@code junit-platform.properties} in |
| 38 | * the root of the class path). Consult the User Guide for further information. |
| 39 | * |
| 40 | * <h2>Use Cases</h2> |
| 41 | * <p>Setting the test instance lifecycle mode to {@link Lifecycle#PER_CLASS |
| 42 | * PER_CLASS} enables the following features. |
| 43 | * <ul> |
| 44 | * <li>Shared test instance state between test methods in a given test class |
| 45 | * as well as between non-static {@link BeforeAll @BeforeAll} and |
| 46 | * {@link AfterAll @AfterAll} methods in the test class.</li> |
| 47 | * <li>Declaration of non-static {@code @BeforeAll} and {@code @AfterAll} methods |
| 48 | * in {@link Nested @Nested} test classes. Beginning with Java 16, {@code @BeforeAll} |
| 49 | * and {@code @AfterAll} methods may be declared as {@code static} in |
| 50 | * {@link Nested @Nested} test classes with either lifecycle mode.</li> |
| 51 | * <li>Declaration of {@code @BeforeAll} and {@code @AfterAll} on interface |
| 52 | * {@code default} methods.</li> |
| 53 | * <li>Simplified declaration of non-static {@code @BeforeAll} and {@code @AfterAll} |
| 54 | * lifecycle methods as well as {@code @MethodSource} factory methods in test classes |
| 55 | * implemented with the Kotlin programming language.</li> |
| 56 | * </ul> |
| 57 | * |
| 58 | * <p>{@code @TestInstance} may also be used as a meta-annotation in order to |
| 59 | * create a custom <em>composed annotation</em> that inherits the semantics |
| 60 | * of {@code @TestInstance}. |
| 61 | * |
| 62 | * @since 5.0 |
| 63 | * @see Nested @Nested |
| 64 | */ |
| 65 | @Target(ElementType.TYPE) |
| 66 | @Retention(RetentionPolicy.RUNTIME) |
| 67 | @Inherited |
| 68 | @Documented |
| 69 | @API(status = STABLE, since = "5.0") |
| 70 | public @interface TestInstance { |
| 71 | |
| 72 | /** |
| 73 | * Enumeration of test instance lifecycle <em>modes</em>. |
| 74 | * |
| 75 | * @see #PER_METHOD |
| 76 | * @see #PER_CLASS |
| 77 | */ |
| 78 | enum Lifecycle { |
| 79 | |
| 80 | /** |
| 81 | * When using this mode, a new test instance will be created once per |
| 82 | * test class. |
| 83 | * |
| 84 | * @see #PER_METHOD |
| 85 | */ |
| 86 | PER_CLASS, |
| 87 | |
| 88 | /** |
| 89 | * When using this mode, a new test instance will be created for each |
| 90 | * test method, test factory method, or test template method. |
| 91 | * |
| 92 | * <p>This mode is analogous to the behavior found in JUnit versions 1 |
| 93 | * through 4. |
| 94 | * |
| 95 | * @see #PER_CLASS |
| 96 | */ |
| 97 | PER_METHOD; |
| 98 | |
| 99 | /** |
| 100 | * Property name used to set the default test instance lifecycle mode: |
| 101 | * {@value} |
| 102 | * |
| 103 | * <h4>Supported Values</h4> |
| 104 | * |
| 105 | * <p>Supported values include names of enum constants defined in |
| 106 | * {@link org.junit.jupiter.api.TestInstance.Lifecycle}, ignoring case. |
| 107 | * |
| 108 | * <p>If not specified, the default is "per_method" which corresponds to |
| 109 | * {@code @TestInstance(Lifecycle.PER_METHOD)}. |
| 110 | * |
| 111 | * @since 5.0 |
| 112 | * @see org.junit.jupiter.api.TestInstance |
| 113 | */ |
| 114 | @API(status = STABLE, since = "5.9") |
| 115 | public static final String DEFAULT_LIFECYCLE_PROPERTY_NAME = "junit.jupiter.testinstance.lifecycle.default"; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * The test instance lifecycle <em>mode</em> to use. |
| 121 | */ |
| 122 | Lifecycle value(); |
| 123 | |
| 124 | } |