多模块Maven项目EllaService集成测试报错排查与解决
Hey there! Let’s work through these integration test issues step by step—they’re super common when setting up Spring-based tests in multi-module Maven projects, so we’ll get this sorted in no time.
First Issue: WebDelegatingSmartContextLoader can’t detect default configurations
This error pops up because your integration test is looking for Spring’s default configuration sources (like application.properties or annotated @Configuration classes) but can’t find them, usually due to:
- A mismatch between your test class’s package structure and your configuration classes’ packages
- Missing dependencies on the module that contains your core config classes
- Not explicitly telling the test which configuration classes to use
Fixes to try:
- Explicitly specify your configuration classes in the test using
@ContextConfiguration(for plain Spring) or@SpringBootTest(classes = ...)(for Spring Boot):// For Spring Boot projects @SpringBootTest(classes = {EllaConfiguration.class, EllaServiceConfiguration.class}) public class EllaServiceIT { ... } // For non-Spring Boot Spring projects @RunWith(SpringRunner.class) @ContextConfiguration(classes = {EllaConfiguration.class, EllaServiceConfiguration.class}) public class EllaServiceIT { ... } - Verify your Maven multi-module dependencies: Make sure your test module’s
pom.xmlincludes a dependency on the core module that holdsEllaConfigurationandEllaService:<dependency> <groupId>your-group-id</groupId> <artifactId>core-module-artifact</artifactId> <version>${project.version}</version> <scope>test</scope> </dependency> - Align package structures: If your test class is in a different package than your configuration classes, add
@ComponentScan(basePackages = "your.config.package")to your test class or configuration classes to ensure Spring can find the beans.
Second Issue: Missing required configuration property 'ella.uri'
Once you’ve fixed the context loader issue, this error means your Spring context is trying to bind the ella.uri property but can’t find a value for it. This happens because test environments don’t automatically pick up production configs, and you haven’t provided a test-specific value.
Fixes to try:
- Add a test-specific properties file: Create a
src/test/resources/application-test.properties(orapplication.properties) file in your test module and add the property:
If usingella.uri=http://your-test-endpoint.com@SpringBootTest, you can specify the profile to use with@ActiveProfiles("test")to load this file. - Inject the property directly in the test annotation: Skip the properties file entirely by passing the property directly to your test annotation:
@SpringBootTest( classes = {EllaConfiguration.class, EllaServiceConfiguration.class}, properties = {"ella.uri=http://localhost:8080/test-ella"} ) public class EllaServiceIT { ... } // For non-Spring Boot projects @RunWith(SpringRunner.class) @ContextConfiguration(classes = {EllaConfiguration.class, EllaServiceConfiguration.class}) @TestPropertySource(properties = {"ella.uri=http://localhost:8080/test-ella"}) public class EllaServiceIT { ... } - Double-check your configuration binding: Ensure your
EllaConfigurationclass is properly set up to bind theella.uriproperty. For example:
Also, make sure you’ve enabled configuration properties with@Configuration @ConfigurationProperties(prefix = "ella") public class EllaConfiguration { private String uri; // Getter and setter required // ... rest of your config }@EnableConfigurationProperties(EllaConfiguration.class)in one of your@Configurationclasses.
Full Working Test Example
Here’s a complete example that combines both fixes for a Spring Boot project:
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertNotNull; @SpringBootTest( classes = {EllaConfiguration.class, EllaServiceConfiguration.class}, properties = {"ella.uri=http://test-ella.example.com"} ) public class EllaServiceIT { @Autowired private EllaService ellaService; @Test public void testEllaServiceFunctionality() { // Replace with your actual test logic String testResult = ellaService.performOperation(); assertNotNull(testResult, "EllaService should return a non-null result"); } }
内容的提问来源于stack exchange,提问作者Arefe

