Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.IllegalStateException with @NestedTestConfiguration #28611

Closed
sergey-morenets opened this issue Jun 13, 2022 · 3 comments
Closed

java.lang.IllegalStateException with @NestedTestConfiguration #28611

sergey-morenets opened this issue Jun 13, 2022 · 3 comments
Assignees
Labels
in: test Issues in the test module status: invalid An issue that we don't feel is valid

Comments

@sergey-morenets
Copy link

sergey-morenets commented Jun 13, 2022

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 13, 2022
@sbrannen sbrannen added in: test Issues in the test module status: invalid An issue that we don't feel is valid labels Jun 13, 2022
@sbrannen
Copy link
Member

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.

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Jun 13, 2022
@sbrannen sbrannen self-assigned this Jun 13, 2022
@sbrannen sbrannen removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 13, 2022
@sergey-morenets
Copy link
Author

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.

@sbrannen
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants