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

Support non-default constructors for DisplayNameGenerator implementations #3557

Closed
wazzeps opened this issue Nov 16, 2023 · 3 comments
Closed

Comments

@wazzeps
Copy link

wazzeps commented Nov 16, 2023

It would be nice to be able to pass an object to the generator implementation constructor, which can receive test methods names from an external system.

This object could be used in other parts of the project, or even managed by DI framework context. That is why a constructor is preferable to creating it in a generator class.

@sbrannen
Copy link
Member

sbrannen commented Nov 16, 2023

It would be nice to be able to pass an object to the generator implementation constructor, which can receive test methods names from an external system.

Who would pass the object to the generator's constructor?

Why can't you just look up the "test method names" within the default constructor (or some collaborator to which the default constructor delegates)?

This object could be used in other parts of the project, or even managed by DI framework context. That is why a constructor is preferable to creating it in a generator class.

There is no "DI framework" per se in JUnit Jupiter.

Are you perhaps referring to ParameterResolver support?

@sbrannen sbrannen changed the title Non-default constructor for DisplayNameGenerator implementation Suppor non-default constructors for DisplayNameGenerator implementations Nov 16, 2023
@wazzeps
Copy link
Author

wazzeps commented Nov 16, 2023

In my case, I use Spring context which manages a bean that provides access to an external system. Let's call it ExternalSystem.

Then I would like to inject it through a constructor into the generator class to get test method names in the overridden generator methods.

private final ExternalSystem externalSystem;

@Autowired
public ExternalSystemDisplayNameGenerator(ExternalSystem externalSystem) {
    this.externalSystem = externalSystem;
}

...

@Override
public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod) {
    return externalSystem.getTestName(testMethod);
}

@wazzeps wazzeps changed the title Suppor non-default constructors for DisplayNameGenerator implementations Support non-default constructors for DisplayNameGenerator implementations Nov 16, 2023
@marcphilipp
Copy link
Member

Team decision: DisplayNameGenerators are invoked at discovery time, i.e. before the Spring extension is created and has a chance to create an application context that would be a prerequisite for doing DI. Therefore, this isn't possible with the current architecture.

@marcphilipp marcphilipp closed this as not planned Won't fix, can't repro, duplicate, stale Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants