| 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.extension; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import java.util.List; |
| 16 | import java.util.Optional; |
| 17 | |
| 18 | import org.apiguardian.api.API; |
| 19 | |
| 20 | /** |
| 21 | * {@code TestInstances} encapsulates the <em>test instances</em> of a test. |
| 22 | * |
| 23 | * <p>While top-level tests only have a single test instance, nested tests |
| 24 | * have one additional instance for each enclosing test class. |
| 25 | * |
| 26 | * @since 5.4 |
| 27 | * @see ExtensionContext#getTestInstances() |
| 28 | * @see ExtensionContext#getRequiredTestInstances() |
| 29 | */ |
| 30 | @API(status = STABLE, since = "5.7") |
| 31 | public interface TestInstances { |
| 32 | |
| 33 | /** |
| 34 | * Get the innermost test instance. |
| 35 | * |
| 36 | * <p>The innermost instance is the one closest to the test method. |
| 37 | * |
| 38 | * @return the innermost test instance; never {@code null} |
| 39 | */ |
| 40 | Object getInnermostInstance(); |
| 41 | |
| 42 | /** |
| 43 | * Get the enclosing test instances, excluding the innermost test instance, |
| 44 | * ordered from outermost to innermost. |
| 45 | * |
| 46 | * @return the enclosing test instances; never {@code null} or containing |
| 47 | * {@code null}, but potentially empty |
| 48 | */ |
| 49 | List<Object> getEnclosingInstances(); |
| 50 | |
| 51 | /** |
| 52 | * Get all test instances, ordered from outermost to innermost. |
| 53 | * |
| 54 | * @return all test instances; never {@code null}, containing {@code null}, |
| 55 | * or empty |
| 56 | */ |
| 57 | List<Object> getAllInstances(); |
| 58 | |
| 59 | /** |
| 60 | * Find the first test instance that is an instance of the supplied required |
| 61 | * type, checking from innermost to outermost. |
| 62 | * |
| 63 | * @param requiredType the type to search for |
| 64 | * @return the first test instance of the required type; never {@code null} |
| 65 | * but potentially empty |
| 66 | */ |
| 67 | <T> Optional<T> findInstance(Class<T> requiredType); |
| 68 | |
| 69 | } |