junit-jupiter-api/src/main/java/org/junit/jupiter/api/extension/TestInstanceFactory.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.extension; |
| 12 | |
| 13 | import static org.apiguardian.api.API.Status.STABLE; |
| 14 | |
| 15 | import org.apiguardian.api.API; |
| 16 | |
| 17 | /** |
| 18 | * {@code TestInstanceFactory} defines the API for {@link Extension |
| 19 | * Extensions} that wish to {@linkplain #createTestInstance create} test instances. |
| 20 | * |
| 21 | * <p>Common use cases include creating test instances with special construction |
| 22 | * requirements or acquiring the test instance from a dependency injection |
| 23 | * framework. |
| 24 | * |
| 25 | * <p>Extensions that implement {@code TestInstanceFactory} must be registered |
| 26 | * at the class level. |
| 27 | * |
| 28 | * <h2>Warning</h2> |
| 29 | * |
| 30 | * <p>Only one {@code TestInstanceFactory} is allowed to be registered for any |
| 31 | * given test class. Registering multiple factories for any single test class |
| 32 | * will result in an exception being thrown for all tests in that class, in any |
| 33 | * subclass, and in any nested class. Note that any {@code TestInstanceFactory} |
| 34 | * registered in a {@linkplain Class#getSuperclass() superclass} or |
| 35 | * {@linkplain Class#getEnclosingClass() enclosing} class (i.e., in the case of |
| 36 | * a {@code @Nested} test class) is <em>inherited</em>. It is therefore the |
| 37 | * user's responsibility to ensure that only a single {@code TestInstanceFactory} |
| 38 | * is registered for any specific test class. |
| 39 | * |
| 40 | * <h2>Constructor Requirements</h2> |
| 41 | * |
| 42 | * <p>Consult the documentation in {@link Extension} for details on |
| 43 | * constructor requirements. |
| 44 | * |
| 45 | * @since 5.3 |
| 46 | * @see #createTestInstance(TestInstanceFactoryContext, ExtensionContext) |
| 47 | * @see TestInstanceFactoryContext |
| 48 | * @see TestInstancePostProcessor |
| 49 | * @see TestInstancePreDestroyCallback |
| 50 | * @see ParameterResolver |
| 51 | */ |
| 52 | @FunctionalInterface |
| 53 | @API(status = STABLE, since = "5.7") |
| 54 | public interface TestInstanceFactory extends Extension { |
| 55 | |
| 56 | /** |
| 57 | * Callback for creating a test instance for the supplied context. |
| 58 | * |
| 59 | * <p><strong>Note</strong>: the {@code ExtensionContext} supplied to a |
| 60 | * {@code TestInstanceFactory} will always return an empty |
| 61 | * {@link java.util.Optional} value from |
| 62 | * {@link ExtensionContext#getTestInstance() getTestInstance()} since the |
| 63 | * test instance cannot exist before it has been created by a |
| 64 | * {@code TestInstanceFactory} or the framework itself. |
| 65 | * |
| 66 | * @param factoryContext the context for the test class to be instantiated; |
| 67 | * never {@code null} |
| 68 | * @param extensionContext the current extension context; never {@code null} |
| 69 | * @return the test instance; never {@code null} |
| 70 | * @throws TestInstantiationException if an error occurs while creating the |
| 71 | * test instance |
| 72 | */ |
| 73 | Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext) |
| 74 | throws TestInstantiationException; |
| 75 | |
| 76 | } |