| 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.MAINTAINED; |
| 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 | import org.junit.platform.commons.annotation.Testable; |
| 23 | |
| 24 | /** |
| 25 | * {@code @TestFactory} is used to signal that the annotated method is a |
| 26 | * <em>test factory</em> method. |
| 27 | * |
| 28 | * <p>In contrast to {@link Test @Test} methods, a test factory is not itself |
| 29 | * a test case but rather a factory for test cases. |
| 30 | * |
| 31 | * <p>{@code @TestFactory} methods must not be {@code private} or {@code static} |
| 32 | * and must return a {@code Stream}, {@code Collection}, {@code Iterable}, |
| 33 | * {@code Iterator}, or array of {@link DynamicNode} instances. Supported |
| 34 | * subclasses of {@code DynamicNode} include {@link DynamicContainer} and |
| 35 | * {@link DynamicTest}. <em>Dynamic tests</em> will be executed lazily, |
| 36 | * enabling dynamic and even non-deterministic generation of test cases. |
| 37 | * |
| 38 | * <p>Any {@code Stream} returned by a {@code @TestFactory} will be properly |
| 39 | * closed by calling {@code stream.close()}, making it safe to use a resource |
| 40 | * such as {@code Files.lines()} as the initial source of the stream. |
| 41 | * |
| 42 | * <p>{@code @TestFactory} methods may optionally declare parameters to be |
| 43 | * resolved by {@link org.junit.jupiter.api.extension.ParameterResolver |
| 44 | * ParameterResolvers}. |
| 45 | * |
| 46 | * <h2>Test Execution Order</h2> |
| 47 | * |
| 48 | * <p>By default, test methods will be ordered using an algorithm that is |
| 49 | * deterministic but intentionally nonobvious. This ensures that subsequent runs |
| 50 | * of a test suite execute test methods in the same order, thereby allowing for |
| 51 | * repeatable builds. In this context, a <em>test method</em> is any instance |
| 52 | * method that is directly annotated or meta-annotated with {@code @Test}, |
| 53 | * {@code @RepeatedTest}, {@code @ParameterizedTest}, {@code @TestFactory}, or |
| 54 | * {@code @TestTemplate}. |
| 55 | * |
| 56 | * <p>Although true <em>unit tests</em> typically should not rely on the order |
| 57 | * in which they are executed, there are times when it is necessary to enforce |
| 58 | * a specific test method execution order — for example, when writing |
| 59 | * <em>integration tests</em> or <em>functional tests</em> where the sequence of |
| 60 | * the tests is important, especially in conjunction with |
| 61 | * {@link TestInstance @TestInstance(Lifecycle.PER_CLASS)}. |
| 62 | * |
| 63 | * <p>To control the order in which test methods are executed, annotate your |
| 64 | * test class or test interface with {@link TestMethodOrder @TestMethodOrder} |
| 65 | * and specify the desired {@link MethodOrderer} implementation. |
| 66 | * |
| 67 | * @since 5.0 |
| 68 | * @see Test |
| 69 | * @see DynamicNode |
| 70 | * @see DynamicTest |
| 71 | * @see DynamicContainer |
| 72 | */ |
| 73 | @Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD }) |
| 74 | @Retention(RetentionPolicy.RUNTIME) |
| 75 | @Documented |
| 76 | @API(status = MAINTAINED, since = "5.3") |
| 77 | @Testable |
| 78 | public @interface TestFactory { |
| 79 | } |