spring-modulith icon indicating copy to clipboard operation
spring-modulith copied to clipboard

ApplicationModuleTest with Modulith fails due to configuration import outside application package

Open ghost opened this issue 3 months ago • 3 comments

The following is a simplified version of the issue I have. If I consider the following tree structure

└── root
    │
    ├── modulea
    │   └── package-info.java
    │
    └── moduleb
        ├── AnythingInModuleB.java
        ├── ModuleBConfiguration.java
        └── package-info.java

where I specify in package-info.java ApplicationModule with id modulea and moduleb respectively. Now the content of ModuleBConfiguration is the following (which of course is just for example)

@Configuration
public class ModuleBConfiguration {
    @Bean
    Integer two() {
        return 2;
    }

    @Bean
    AnythingInModuleB anythingInModule() {
        return new AnythingInModuleB();
    }

}

And now in tests I have the following structure

.
└── root
    ├── TestConfiguration.java
    └── modulea
        ├── TestImportB.java

with TestConfiguration annotated with Modulith and TestImportB with the following content

@ApplicationModuleTest
@Import({ModuleBConfiguration.class})
public class TestBeanOutsideB {
    @Test
    void testBeanOutsideB() {}
}

The initialization of this fails with

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'two' defined in class path resource [root/moduleb/ModuleBConfiguration.class]: Unexpected exception during bean creation
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'root.moduleb.ModuleBConfiguration' available

Where does this come from ? Is this expected ? And what surprised me most, is that I only get this is issue with Beans in the configuration that are outside of package root (e.g. Integer), that is AnythingInModuleB works fine.

What is the real use case for this ?

The real use case for this is actually a library that contains two modules and I want to provide both modules with an AutoConfiguration. Hence, I have a structure with a package that contains two modules, both have one auto-configuration (applied through META-INF.spring). In integration tests I set a @Modulith tag to a file in the root package. I considered having instead files with @Modulith tag inside the modules for testing, but then the recognition of the different modules does not work.

ghost avatar Sep 24 '25 13:09 ghost