| 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.reflect.Method; |
| 16 | import java.util.Optional; |
| 17 | import java.util.Set; |
| 18 | |
| 19 | import org.apiguardian.api.API; |
| 20 | |
| 21 | /** |
| 22 | * {@code TestInfo} is used to inject information about the current test or |
| 23 | * container into to {@code @Test}, {@code @RepeatedTest}, |
| 24 | * {@code @ParameterizedTest}, {@code @TestFactory}, {@code @BeforeEach}, |
| 25 | * {@code @AfterEach}, {@code @BeforeAll}, and {@code @AfterAll} methods. |
| 26 | * |
| 27 | * <p>If a method parameter is of type {@link TestInfo}, JUnit will supply |
| 28 | * an instance of {@code TestInfo} corresponding to the current test or |
| 29 | * container as the value for the parameter. |
| 30 | * |
| 31 | * @since 5.0 |
| 32 | * @see Test |
| 33 | * @see RepeatedTest |
| 34 | * @see TestFactory |
| 35 | * @see BeforeEach |
| 36 | * @see AfterEach |
| 37 | * @see BeforeAll |
| 38 | * @see AfterAll |
| 39 | * @see DisplayName |
| 40 | * @see Tag |
| 41 | */ |
| 42 | @API(status = STABLE, since = "5.0") |
| 43 | public interface TestInfo { |
| 44 | |
| 45 | /** |
| 46 | * Get the display name of the current test or container. |
| 47 | * |
| 48 | * <p>The display name is either a default name or a custom name configured |
| 49 | * via {@link DisplayName @DisplayName}. |
| 50 | * |
| 51 | * <h4>Default Display Names</h4> |
| 52 | * |
| 53 | * <p>If the context in which {@code TestInfo} is used is at the container |
| 54 | * level, the default display name is generated based on the name of the |
| 55 | * test class. For top-level and {@link Nested @Nested} test classes, the |
| 56 | * default display name is the {@linkplain Class#getSimpleName simple name} |
| 57 | * of the class. For {@code static} nested test classes, the default display |
| 58 | * name is the default display name for the enclosing class concatenated with |
| 59 | * the {@linkplain Class#getSimpleName simple name} of the {@code static} |
| 60 | * nested class, separated by a dollar sign ({@code $}). For example, the |
| 61 | * default display names for the following test classes are |
| 62 | * {@code TopLevelTests}, {@code NestedTests}, and {@code TopLevelTests$StaticTests}. |
| 63 | * |
| 64 | * <pre class="code"> |
| 65 | * class TopLevelTests { |
| 66 | * |
| 67 | * {@literal @}Nested |
| 68 | * class NestedTests {} |
| 69 | * |
| 70 | * static class StaticTests {} |
| 71 | * } |
| 72 | * </pre> |
| 73 | * |
| 74 | * <p>If the context in which {@code TestInfo} is used is at the test level, |
| 75 | * the default display name is the name of the test method concatenated with |
| 76 | * a comma-separated list of {@linkplain Class#getSimpleName simple names} |
| 77 | * of the parameter types in parentheses. For example, the default display |
| 78 | * name for the following test method is {@code testUser(TestInfo, User)}. |
| 79 | * |
| 80 | * <pre class="code"> |
| 81 | * {@literal @}Test |
| 82 | * void testUser(TestInfo testInfo, {@literal @}Mock User user) {} |
| 83 | * </pre> |
| 84 | * |
| 85 | * <p>Note that display names are typically used for test reporting in IDEs |
| 86 | * and build tools and may contain spaces, special characters, and even emoji. |
| 87 | * |
| 88 | * @return the display name of the test or container; never {@code null} or blank |
| 89 | */ |
| 90 | String getDisplayName(); |
| 91 | |
| 92 | /** |
| 93 | * Get the set of all tags for the current test or container. |
| 94 | * |
| 95 | * <p>Tags may be declared directly on the test element or <em>inherited</em> |
| 96 | * from an outer context. |
| 97 | */ |
| 98 | Set<String> getTags(); |
| 99 | |
| 100 | /** |
| 101 | * Get the {@link Class} associated with the current test or container, if available. |
| 102 | */ |
| 103 | Optional<Class<?>> getTestClass(); |
| 104 | |
| 105 | /** |
| 106 | * Get the {@link Method} associated with the current test or container, if available. |
| 107 | */ |
| 108 | Optional<Method> getTestMethod(); |
| 109 | |
| 110 | } |