diff --git a/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTestAutoConfiguration.java b/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTestAutoConfiguration.java index 237b4917..86aad2c8 100644 --- a/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTestAutoConfiguration.java +++ b/spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTestAutoConfiguration.java @@ -43,8 +43,8 @@ import org.springframework.util.StringUtils; @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE) class ModuleTestAutoConfiguration { - private static final String AUTOCONFIG_PACKAGES = "org.springframework.boot.autoconfigure.AutoConfigurationPackages"; - private static final String ENTITY_SCAN_PACKAGE = "org.springframework.boot.autoconfigure.domain.EntityScanPackages"; + static final String AUTOCONFIG_PACKAGES = "org.springframework.boot.autoconfigure.AutoConfigurationPackages"; + static final String ENTITY_SCAN_PACKAGE = "org.springframework.boot.autoconfigure.domain.EntityScanPackages"; static class AutoConfigurationAndEntityScanPackageCustomizer implements ImportBeanDefinitionRegistrar { @@ -67,7 +67,7 @@ class ModuleTestAutoConfiguration { setBasePackagesOn(registry, ENTITY_SCAN_PACKAGE, basePackages); } - private void setBasePackagesOn(BeanDefinitionRegistry registry, String beanName, List packages) { + void setBasePackagesOn(BeanDefinitionRegistry registry, String beanName, List packages) { if (!registry.containsBeanDefinition(beanName)) { return; @@ -77,9 +77,11 @@ class ModuleTestAutoConfiguration { var definition = registry.getBeanDefinition(beanName); var holder = definition.getConstructorArgumentValues().getArgumentValue(0, String[].class); - Arrays.stream((String[]) holder.getValue()) - .filter(it -> it.startsWith("org.springframework.modulith")) - .forEach(packagesToSet::add); + if (holder != null) { + Arrays.stream((String[]) holder.getValue()) + .filter(it -> it.startsWith("org.springframework.modulith")) + .forEach(packagesToSet::add); + } definition.getConstructorArgumentValues().addIndexedArgumentValue(0, packagesToSet.toArray(String[]::new)); } diff --git a/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleTestAutoConfigurationUnitTests.java b/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleTestAutoConfigurationUnitTests.java new file mode 100644 index 00000000..eda27e00 --- /dev/null +++ b/spring-modulith-test/src/test/java/org/springframework/modulith/test/ModuleTestAutoConfigurationUnitTests.java @@ -0,0 +1,46 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.modulith.test; + +import static org.assertj.core.api.Assertions.*; + +import java.util.Collections; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.boot.autoconfigure.AutoConfigurationPackages; + +/** + * Unit tests for {@link ModuleTestAutoConfiguration}. + * + * @author Oliver Drotbohm + */ +class ModuleTestAutoConfigurationUnitTests { + + @Test // GH-453 + void handlesNoConstructorArgumentOnPackagesBeanDefinition() { + + var definition = new RootBeanDefinition(AutoConfigurationPackages.class); + var factory = new DefaultListableBeanFactory(); + factory.registerBeanDefinition(ModuleTestAutoConfiguration.AUTOCONFIG_PACKAGES, definition); + + assertThatNoException().isThrownBy(() -> { + new ModuleTestAutoConfiguration.AutoConfigurationAndEntityScanPackageCustomizer() + .setBasePackagesOn(factory, ModuleTestAutoConfiguration.AUTOCONFIG_PACKAGES, Collections.emptyList()); + }); + } +}