Skip to content

Commit e2cc4fe

Browse files
committed
GH-345 - Properly handle manually declared entity scan and autoconfiguration packages.
If Spring Modulith packages were explicitly configured as autoconfiguration or entity scan packages, the test autoconfiguration would fail as it previously attempted to manipulate an immutable list. We now create a copy of that list to fix this.
1 parent 4e45388 commit e2cc4fe

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

spring-modulith-test/src/main/java/org/springframework/modulith/test/ModuleTestAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.modulith.test;
1717

18+
import java.util.ArrayList;
1819
import java.util.Arrays;
1920
import java.util.List;
2021

@@ -72,14 +73,15 @@ private void setBasePackagesOn(BeanDefinitionRegistry registry, String beanName,
7273
return;
7374
}
7475

76+
var packagesToSet = new ArrayList<>(packages);
7577
var definition = registry.getBeanDefinition(beanName);
7678
var holder = definition.getConstructorArgumentValues().getArgumentValue(0, String[].class);
7779

7880
Arrays.stream((String[]) holder.getValue())
7981
.filter(it -> it.startsWith("org.springframework.modulith"))
80-
.forEach(packages::add);
82+
.forEach(packagesToSet::add);
8183

82-
definition.getConstructorArgumentValues().addIndexedArgumentValue(0, packages.toArray(String[]::new));
84+
definition.getConstructorArgumentValues().addIndexedArgumentValue(0, packagesToSet.toArray(String[]::new));
8385
}
8486
}
8587
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.test;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.boot.autoconfigure.AutoConfigurations;
22+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
import org.springframework.boot.autoconfigure.domain.EntityScan;
25+
import org.springframework.boot.autoconfigure.domain.EntityScanPackages;
26+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
27+
28+
/**
29+
* Integration tests for {@link ModuleTestAutoConfiguration}.
30+
*
31+
* @author Oliver Drotbohm
32+
*/
33+
class ModuleTestAutoConfigurationIntegrationTests {
34+
35+
@Test // GH-345
36+
void bootstrapsTestWithManualEntityScanOfModulithPackage() {
37+
38+
new ApplicationContextRunner()
39+
.withBean(ModuleTestExecution.class, ModuleTestExecution.of(ModuleTest.class))
40+
.withConfiguration(AutoConfigurations.of(ModuleTestAutoConfiguration.class))
41+
.withUserConfiguration(ManualModulithEntityScan.class)
42+
.run(ctx -> {
43+
44+
assertThat(ctx).hasNotFailed();
45+
assertThat(ctx.getBean(EntityScanPackages.class).getPackageNames())
46+
.containsExactlyInAnyOrder("org.springframework.modulith.test",
47+
"org.springframework.modulith.events.jpa");
48+
});
49+
}
50+
51+
@SpringBootApplication
52+
@ApplicationModuleTest
53+
static class ModuleTest {}
54+
55+
@EnableAutoConfiguration
56+
@EntityScan("org.springframework.modulith.events.jpa")
57+
static class ManualModulithEntityScan {}
58+
}

0 commit comments

Comments
 (0)