spring-projects / spring-projects/spring-modulith

ApplicationModuleTest with Modulith fails due to configuration import outside application package

Open
#1,381 3 comments 0 reactions 1 assignee View on GitHub

@odrotbohm is already working on this.

Since Sep 24, 2025.

in: core in: test support meta: waiting for feedback
Dominant language
Java
Stars
1.2k
Forks
222
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.