Hi

I tried to use @NestedTestConfiguration as described in the Spring documentation. So this is the original test class that works properly:

@SpringJUnitConfig(AppConfig.class)
@TestPropertySource(properties = { "db.port=7000", "db.name=local" })
public class ServerTest {

    @Nested
    @TestPropertySource(properties = "db.port=8000")
    public class ServerLoadConfiguration {

Then I want to override my Spring configuration in the inner class and I try to put @NestedTestConfiguration(EnclosingConfiguration.OVERRIDE) either on ServerLoadConfiguration class or ServerTest class, but in both cases I always get an exception:

Caused by: java.lang.IllegalStateException: Neither GenericXmlContextLoader nor AnnotationConfigContextLoader was able to load an ApplicationContext from [MergedContextConfiguration@5ef6ae06 testClass = ServerTest.ServerLoadConfiguration, locations = '{}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{db.port=8000}', contextCustomizers = set[[empty]], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]].
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:255)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:123)

Spring version: 6.0.0-M4

Comment From: sbrannen

This is the expected behavior.

If you declare @NestedTestConfiguration(OVERRIDE), none of your Spring configuration (annotations) will be inherited from the enclosing class.

In your concrete example, you specify AppConfig.class as the configuration class via @SpringJUnitConfig, but you don't have any @ContextConfiguration or @SpringJUnitConfig annotation declared on ServerLoadConfiguration.

That's why you see classes = '{}' in the MergedContextConfiguration in the exception message.

If you want to inherit the configuration classes and only override the test properties, you should be able to achieve that as follows:


@Nested
@TestPropertySource(properties = "db.port=8000", inheritProperties = false)
public class ServerLoadConfiguration { /* ... */ }

In light of the above, I am closing this issue, but feel free to post back here with additional comments in case you still run into issues.

Comment From: sergey-morenets

Hi @sbrannen

Thank you for the quick response and detailed explanation. I would add a sample code snippet with @NestedTestConfiguration in the documentaiton: https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html

Currently I couldn't find any code example there.

Comment From: sbrannen

Thank you for the quick response and detailed explanation.

You're welcome.

I would add a sample code snippet with @NestedTestConfiguration in the documentaiton: https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html

Currently I couldn't find any code example there.

There is an example of configuration being automatically inherited, but you're right: there's no example demonstrating how to use @NestedTestConfiguration. So I'll add that.