junit5 icon indicating copy to clipboard operation
junit5 copied to clipboard

DisplayNameGenerator should be able to fall back to default

Open d-william opened this issue 2 years ago • 8 comments

Motivation :
Having the configuration can be useful to fallback on the default DisplayNameGenerator

Example :

@DisplayNameGeneration(MyTest.MyDisplayNameGenerator.class)
public class MyTest {

    @Test
    @MyDisplayName("My display name")
    public void test1() {
        // a test
    }

    @Test
    public void test2() {
        // a test
    }

    static class MyDisplayNameGenerator implements DisplayNameGenerator {

        @Override
        public String generateDisplayNameForClass(Class<?> testClass, JupiterConfiguration configuration) {
            return configuration.getDefaultDisplayNameGenerator().generateDisplayNameForClass(testClass, configuration);
        }

        @Override
        public String generateDisplayNameForNestedClass(Class<?> nestedClass, JupiterConfiguration configuration) {
            return configuration.getDefaultDisplayNameGenerator().generateDisplayNameForNestedClass(nestedClass, configuration);
        }

        @Override
        public String generateDisplayNameForMethod(Class<?> testClass, Method testMethod, JupiterConfiguration configuration) {
            Optional<MyDisplayName> optional = findAnnotation(testMethod, MyDisplayName.class);
            return optional.map(MyDisplayName::value)
                    .orElseGet(() -> configuration.getDefaultDisplayNameGenerator().generateDisplayNameForMethod(testClass, testMethod, configuration));
        }
        
    }

}

d-william avatar Jul 06 '22 19:07 d-william

JupiterConfiguration is an internal class in junit-jupiter-engine so it cannot be passed as-is to DisplayNameGenerator which is in junit-jupiter-api. But I can see that falling back to the default would be useful. I think the smallest API change would be to allow DisplayNameGenerators to return null which would then be checked in the engine code.

@d-william Would that work for you?

marcphilipp avatar Jul 07 '22 05:07 marcphilipp

JupiterConfiguration is an internal class in junit-jupiter-engine so it cannot be passed as-is to DisplayNameGenerator which is in junit-jupiter-api. But I can see that falling back to the default would be useful. I think the smallest API change would be to allow DisplayNameGenerators to return null which would then be checked in the engine code.

@d-william Would that work for you?

Yes 🙂

d-william avatar Jul 07 '22 06:07 d-william

Team Decision: Allow methods in DisplayNameGenerator to return null to signal to fall back to the default generator.

marcphilipp avatar Jul 08 '22 14:07 marcphilipp

@d-william Would you be interested in submitting a PR for this?

marcphilipp avatar Jul 08 '22 14:07 marcphilipp

I could spend some time on this if there are no objections.

gilbertojrequena avatar Jul 08 '22 19:07 gilbertojrequena

@gilbertojrequena Go for it! 👍

marcphilipp avatar Jul 08 '22 20:07 marcphilipp

@d-william Would you be interested in submitting a PR for this?

Sorry for the late reply, I think I can handle it if it is still possible

d-william avatar Jul 09 '22 09:07 d-william

@d-william Would you be interested in submitting a PR for this?

Sorry for the late reply, I think I can handle it if it is still possible

No worries, go ahead :) @marcphilipp could you assign this to @d-william ?

gilbertojrequena avatar Jul 09 '22 12:07 gilbertojrequena