Skip to content

Introduce ExtensionContext.getEnclosingTestClasses() #4376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions documentation/src/docs/asciidoc/release-notes/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ include::{includedir}/link-attributes.adoc[]

include::{basedir}/release-notes-5.13.0-M1.adoc[]

include::{basedir}/release-notes-5.12.1.adoc[]

include::{basedir}/release-notes-5.12.0.adoc[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[[release-notes-5.12.1]]
== 5.12.1

*Date of Release:* ❓

*Scope:* ❓

For a complete list of all _closed_ issues and pull requests for this release, consult the
link:{junit5-repo}+/milestone/91?closed=1+[5.12.1] milestone page in the JUnit repository
on GitHub.


[[release-notes-5.12.1-junit-platform]]
=== JUnit Platform

[[release-notes-5.12.1-junit-platform-bug-fixes]]
==== Bug Fixes

* ❓

[[release-notes-5.12.1-junit-platform-deprecations-and-breaking-changes]]
==== Deprecations and Breaking Changes

* ❓

[[release-notes-5.12.1-junit-platform-new-features-and-improvements]]
==== New Features and Improvements

* ❓


[[release-notes-5.12.1-junit-jupiter]]
=== JUnit Jupiter

[[release-notes-5.12.1-junit-jupiter-bug-fixes]]
==== Bug Fixes

* ❓

[[release-notes-5.12.1-junit-jupiter-deprecations-and-breaking-changes]]
==== Deprecations and Breaking Changes

* ❓

[[release-notes-5.12.1-junit-jupiter-new-features-and-improvements]]
==== New Features and Improvements

* New `ExtensionContext.getEnclosingTestClasses()` method to help with migration away from
`AnnotationSupport.findAnnotation(Class, Class, SearchOption)` (deprecated since 1.12.0)
to `AnnotationSupport.findAnnotation(Class, Class, List)`.


[[release-notes-5.12.1-junit-vintage]]
=== JUnit Vintage

[[release-notes-5.12.1-junit-vintage-bug-fixes]]
==== Bug Fixes

* ❓

[[release-notes-5.12.1-junit-vintage-deprecations-and-breaking-changes]]
==== Deprecations and Breaking Changes

* ❓

[[release-notes-5.12.1-junit-vintage-new-features-and-improvements]]
==== New Features and Improvements

* ❓
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
* <p>{@link Extension Extensions} are provided an instance of
* {@code ExtensionContext} to perform their work.
*
* <p>This interface is not intended to be implemented by clients.
*
* @since 5.0
* @see Store
* @see Namespace
Expand Down Expand Up @@ -129,6 +131,31 @@ public interface ExtensionContext {
*/
Optional<Class<?>> getTestClass();

/**
* Get the enclosing test classes of the current test or container.
*
* <p>This method is useful to look up annotations on nested test classes
* and their enclosing <em>runtime</em> types:
*
* <pre>{@code
* AnnotationSupport.findAnnotation(
* extensionContext.getRequiredTestClass(),
* MyAnnotation.class,
* extensionContext.getEnclosingTestClasses()
* );
* }</pre>
*
* @return an empty list if there is no class associated with the current
* test or container or when it is not nested; otherwise, a list containing
* the enclosing test classes in order from outermost to innermost; never
* {@code null}
*
* @since 5.12.1
* @see org.junit.platform.commons.support.AnnotationSupport#findAnnotation(Class, Class, List)
*/
@API(status = EXPERIMENTAL, since = "5.12.1")
List<Class<?>> getEnclosingTestClasses();

/**
* Get the <em>required</em> {@link Class} associated with the current test
* or container.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.TestInstance.Lifecycle;
Expand Down Expand Up @@ -54,6 +55,11 @@ public Optional<Class<?>> getTestClass() {
return Optional.of(getTestDescriptor().getTestClass());
}

@Override
public List<Class<?>> getEnclosingTestClasses() {
return getTestDescriptor().getEnclosingTestClasses();
}

@Override
public Optional<Lifecycle> getTestInstanceLifecycle() {
return Optional.of(this.lifecycle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.TestInstance.Lifecycle;
Expand Down Expand Up @@ -44,6 +45,11 @@ public Optional<Class<?>> getTestClass() {
return Optional.of(getTestDescriptor().getTestClass());
}

@Override
public List<Class<?>> getEnclosingTestClasses() {
return getTestDescriptor().getEnclosingTestClasses();
}

@Override
public Optional<Lifecycle> getTestInstanceLifecycle() {
return getParent().flatMap(ExtensionContext::getTestInstanceLifecycle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

package org.junit.jupiter.engine.descriptor;

import static java.util.Collections.emptyList;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.TestInstance;
Expand Down Expand Up @@ -40,6 +43,11 @@ public Optional<Class<?>> getTestClass() {
return Optional.empty();
}

@Override
public List<Class<?>> getEnclosingTestClasses() {
return emptyList();
}

@Override
public Optional<TestInstance.Lifecycle> getTestInstanceLifecycle() {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@

package org.junit.jupiter.engine.descriptor;

import static java.util.Collections.emptyList;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.TestInstance.Lifecycle;
Expand Down Expand Up @@ -43,6 +46,11 @@ public Optional<Class<?>> getTestClass() {
return Optional.empty();
}

@Override
public List<Class<?>> getEnclosingTestClasses() {
return emptyList();
}

@Override
public Optional<Lifecycle> getTestInstanceLifecycle() {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
* @since 5.0
*/
@API(status = INTERNAL, since = "5.0")
public abstract class MethodBasedTestDescriptor extends JupiterTestDescriptor implements ResourceLockAware {
public abstract class MethodBasedTestDescriptor extends JupiterTestDescriptor
implements ResourceLockAware, TestClassAware {

private static final Logger logger = LoggerFactory.getLogger(MethodBasedTestDescriptor.class);

Expand Down Expand Up @@ -106,7 +107,8 @@ public Function<ResourceLocksProvider, Set<ResourceLocksProvider.Lock>> getResou
getTestMethod()));
}

private List<Class<?>> getEnclosingTestClasses() {
@Override
public List<Class<?>> getEnclosingTestClasses() {
return getParent() //
.filter(TestClassAware.class::isInstance) //
.map(TestClassAware.class::cast) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.TestInstance.Lifecycle;
Expand Down Expand Up @@ -51,6 +52,11 @@ public Optional<Class<?>> getTestClass() {
return Optional.of(getTestDescriptor().getTestClass());
}

@Override
public List<Class<?>> getEnclosingTestClasses() {
return getTestDescriptor().getEnclosingTestClasses();
}

@Override
public Optional<Lifecycle> getTestInstanceLifecycle() {
return getParent().flatMap(ExtensionContext::getTestInstanceLifecycle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.TestInstance.Lifecycle;
Expand Down Expand Up @@ -47,6 +48,11 @@ public Optional<Class<?>> getTestClass() {
return Optional.of(getTestDescriptor().getTestClass());
}

@Override
public List<Class<?>> getEnclosingTestClasses() {
return getTestDescriptor().getEnclosingTestClasses();
}

@Override
public Optional<Lifecycle> getTestInstanceLifecycle() {
return getParent().flatMap(ExtensionContext::getTestInstanceLifecycle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static <A extends Annotation> Optional<A> findAnnotation(AnnotatedElement
* @since 1.8
* @see SearchOption
* @see #findAnnotation(AnnotatedElement, Class)
* @deprecated Use {@link #findAnnotation(Class, Class, List)}
* @deprecated Use {@link #findAnnotation(AnnotatedElement, Class)}
* (for {@code SearchOption.DEFAULT}) or
* {@link #findAnnotation(Class, Class, List)} (for
* {@code SearchOption.INCLUDE_ENCLOSING_CLASSES}) instead
Expand Down
Loading
Loading