From 658775b914ab753783bcf01762c882f5822f660f Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Wed, 10 Dec 2025 18:51:43 +0100 Subject: [PATCH] Avoid unnecessary list creation & processing in AbstractTestContextBootstrapper Closes gh-35995 --- .../AbstractTestContextBootstrapper.java | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java index d2f4f1310c0..d1a6b189ddc 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java @@ -152,7 +152,9 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot listeners.addAll(getDefaultTestExecutionListeners()); } - listeners.addAll(0, instantiateListeners(testExecutionListeners.listeners())); + if (testExecutionListeners.listeners().length > 0) { + listeners.addAll(0, instantiateListeners(testExecutionListeners.listeners())); + } descriptor = (inheritListeners ? parentDescriptor : null); } @@ -316,17 +318,25 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot } if (contextLoader instanceof SmartContextLoader smartContextLoader) { smartContextLoader.processContextConfiguration(configAttributes); - locations.addAll(0, Arrays.asList(configAttributes.getLocations())); - classes.addAll(0, Arrays.asList(configAttributes.getClasses())); + if (configAttributes.getLocations().length > 0) { + locations.addAll(0, Arrays.asList(configAttributes.getLocations())); + } + if (configAttributes.getClasses().length > 0) { + classes.addAll(0, Arrays.asList(configAttributes.getClasses())); + } } else { @SuppressWarnings("deprecation") String[] processedLocations = contextLoader.processLocations( configAttributes.getDeclaringClass(), configAttributes.getLocations()); - locations.addAll(0, Arrays.asList(processedLocations)); + if (processedLocations.length > 0) { + locations.addAll(0, Arrays.asList(processedLocations)); + } // Legacy ContextLoaders don't know how to process classes } - initializers.addAll(0, Arrays.asList(configAttributes.getInitializers())); + if (configAttributes.getInitializers().length > 0) { + initializers.addAll(0, Arrays.asList(configAttributes.getInitializers())); + } if (!configAttributes.isInheritLocations()) { break; } @@ -401,7 +411,9 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot boolean inheritFactories = annotation.inheritFactories(); AnnotationDescriptor parentDescriptor = descriptor.next(); - factories.addAll(0, instantiateCustomizerFactories(annotation.factories())); + if (annotation.factories().length > 0) { + factories.addAll(0, instantiateCustomizerFactories(annotation.factories())); + } // If there are no factories to inherit, we might need to merge the // locally declared factories with the defaults.