Skip to content
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

@DisplayNameGeneration is not discovered on runtime enclosing type for @Nested test class #4131

Closed
2 tasks done
sbrannen opened this issue Nov 15, 2024 · 9 comments · Fixed by #4268
Closed
2 tasks done

Comments

@sbrannen
Copy link
Member

sbrannen commented Nov 15, 2024

Overview

@DisplayNameGeneration is currently discovered on the current test, a superclass, or the enclosing class in which a Nested test class is declared. However, the runtime type of an enclosing instance is not always the class in which a @Nested test class is declared.

Example

The examples here are copies of those in #4130 except that @IndicativeSentencesGeneration has been moved from AbstractBaseTests to ScenarioOneTests.

abstract class AbstractBaseTests {

	@Nested
	class NestedTests {

		@Test
		void test() {
		}
	}
}
@IndicativeSentencesGeneration
class ScenarioOneTests extends AbstractBaseTests {
}

Actual Behavior

When running ScenarioOneTests, we currently see the following display names, since @IndicativeSentencesGeneration is not honored (i.e., the @DisplayNameGeneration(IndicativeSentences.class) meta-annotation is not discovered).

  • ScenarioOneTests
    • NestedTests
      • test()

Expected Behavior

When running ScenarioOneTests, we would expect the following display names before #4130 is resolved.

  • ScenarioOneTests
    • AbstractBaseTests, NestedTests
      • AbstractBaseTests, NestedTests, test()

When running ScenarioOneTests, we would expect the following display names after #4130 is resolved.

  • ScenarioOneTests
    • ScenarioOneTests, NestedTests
      • ScenarioOneTests, NestedTests, test()

Related Issues

Deliverables

  • Determine if it is possible to support this use case
  • If it is possible, ensure that @DisplayNameGeneration is discovered on the runtime enclosing type for a @Nested test class.
@marcphilipp
Copy link
Member

Team decision: Investigate feasibility after #4130 has been addressed.

@abdelmawla56
Copy link

hello @sbrannen, I would love to work on this issue, would you please assign it to me?
it will be solved by adding somethings

@marcphilipp
Copy link
Member

@abdelmawla56 I'd be happy to but #4130 should be done first because we think there'll be a large overlap. That's why this issue is currently labeled as "blocked".

@snicoll
Copy link

snicoll commented Mar 6, 2025

This change is leading to deprecation warnings when upgrading as we're relying on SearchOption.

Looking at ed68831, it looks like it has been introduced for a very specific feature, which came a bit as a surprise to me. Our use case is to find an annotation that can be specified on a @Nested-annotated type as well as enclosing types.

Given that @Nested is a feature of JUnit, I was expecting some sort of support which is exactly what the (now deprecated) feature did. Have I missed something or do we have to compute the list of types ourselves now? It also feel the reason for the deprecation is quite subtle (the distinction between the compile-time enclosing types and the runtime enclosing types).

@sbrannen
Copy link
Member Author

sbrannen commented Mar 6, 2025

Thanks for bringing this up, @snicoll.

We'll discuss this within the team.

@sbrannen
Copy link
Member Author

sbrannen commented Mar 7, 2025

I searched the Spring Boot code base and discovered the recently introduced ResourcesExtension which uses SearchOption.

@snicoll, can you please confirm that's the use case you are talking about?

Thanks

@wilkinsona
Copy link

Yes, that's where we noticed the deprecation.

I would have liked to use the search capabilities here as well but, IIRC, couldn't do so as we're looking for a repeatable annotation there.

We've struggled with supporting the semantics of @Nested in the past. In this case it's not as important as the code in question is an internal test utility so it only needs to work for our own purposes. More generally, I'd definitely welcome anything that eases supporting @Nested and the now-deprecated search capabilities were a step in that direction.

@marcphilipp
Copy link
Member

Team decision: Introduce ExtensionContext.getEnclosingTestClasses() : List<Class<?>> method for use with AnnotationSupport.findAnnotation(Class, Class, List<Class>) in 5.12.1

@wilkinsona
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants