|
34 | 34 | import org.springframework.modulith.core.ApplicationModules;
|
35 | 35 | import org.springframework.modulith.core.JavaPackage;
|
36 | 36 | import org.springframework.modulith.test.ApplicationModuleTest.BootstrapMode;
|
| 37 | +import org.springframework.util.StringUtils; |
37 | 38 | import org.springframework.util.function.SingletonSupplier;
|
38 | 39 |
|
39 | 40 | /**
|
@@ -94,12 +95,21 @@ public static Supplier<ModuleTestExecution> of(Class<?> type) {
|
94 | 95 |
|
95 | 96 | var annotation = AnnotatedElementUtils.findMergedAnnotation(type, ApplicationModuleTest.class);
|
96 | 97 | var packageName = type.getPackage().getName();
|
| 98 | + var optionalModulithType = findSpringBootApplicationByClasses(annotation); |
97 | 99 |
|
98 |
| - var modulithType = MODULITH_TYPES.computeIfAbsent(type, |
99 |
| - it -> new AnnotatedClassFinder(SpringBootApplication.class).findFromPackage(packageName)); |
| 100 | + var modulithType = optionalModulithType.orElseGet(() -> MODULITH_TYPES.computeIfAbsent(type, |
| 101 | + it -> new AnnotatedClassFinder(SpringBootApplication.class).findFromPackage(packageName))); |
100 | 102 | var modules = ApplicationModules.of(modulithType);
|
101 |
| - var module = modules.getModuleForPackage(packageName).orElseThrow( // |
| 103 | + |
| 104 | + var moduleName = annotation.module(); |
| 105 | + ApplicationModule module; |
| 106 | + if (StringUtils.hasText(moduleName)) { |
| 107 | + module = modules.getModuleByName(moduleName).orElseThrow( // |
| 108 | + () -> new IllegalStateException(String.format("Unable to find module %s!", moduleName))); |
| 109 | + } else { |
| 110 | + module = modules.getModuleForPackage(packageName).orElseThrow( // |
102 | 111 | () -> new IllegalStateException(String.format("Package %s is not part of any module!", packageName)));
|
| 112 | + } |
103 | 113 |
|
104 | 114 | return EXECUTIONS.computeIfAbsent(new Key(module.getBasePackage().getName(), annotation),
|
105 | 115 | it -> new ModuleTestExecution(annotation, modules, module));
|
@@ -230,5 +240,16 @@ private static Stream<ApplicationModule> getExtraModules(ApplicationModuleTest a
|
230 | 240 | .flatMap(Optional::stream);
|
231 | 241 | }
|
232 | 242 |
|
| 243 | + private static Optional<Class<?>> findSpringBootApplicationByClasses(ApplicationModuleTest annotation) { |
| 244 | + for (Class<?> clazz : annotation.classes()) { |
| 245 | + Class<?> modulithType = MODULITH_TYPES.computeIfAbsent(clazz, |
| 246 | + it -> new AnnotatedClassFinder(SpringBootApplication.class).findFromPackage(clazz.getPackageName())); |
| 247 | + if (modulithType != null) { |
| 248 | + return Optional.of(modulithType); |
| 249 | + } |
| 250 | + } |
| 251 | + return Optional.empty(); |
| 252 | + } |
| 253 | + |
233 | 254 | private static record Key(String moduleBasePackage, ApplicationModuleTest annotation) {}
|
234 | 255 | }
|
0 commit comments