Skip to content

@Nested test class does not reuse enclosing test class context (when no customization and @ContextConfiguration) #37189

Description

@seregamorph

Since 7.1.0-M1 spring-test changes the logic calculating the MergedContextConfiguration entity for the nested test class. Consider example:

@SpringBootTest(classes = {
    EnclosingTest.Configuration.class
})
class EnclosingTest {
    class NestedTest {

Before 7.1.0-M1 both EnclosingTest and EnclosingTest.NestedTest had equal MergedContextConfiguration which also means that the same application context is reused between them.

But now it's changed. The MergedContextConfiguration are different, the nested class now has TWO classes in the classes fields (both the same).

Most probably it is related to this change between 7.0 and 7.1 (also doc):

List<ContextConfigurationAttributes> defaultConfigAttributesList =
        Collections.singletonList(new ContextConfigurationAttributes(testClass));
        // for 7.1: ContextLoaderUtils.resolveDefaultContextConfigurationAttributes(testClass);

Crucial detail

After some research it was found, that this depends on the presence of @ContextConfiguration on the enclosing class. If there is a declaration like this:

@SpringBootTest(classes = {
    EnclosingTest.Configuration.class
})
@ContextConfiguration
class EnclosingTest {
    class NestedTest {

then the test cache behaves as expected.

Most probably it works correctly because of this condition in AbstractTestContextBootstrapper:

@Override
public final MergedContextConfiguration buildMergedContextConfiguration() {
...
// is evaluated to false (annotation found) if @ContextConfiguration is present, and the branch skipped
	if (TestContextAnnotationUtils.findAnnotationDescriptorForTypes(
			testClass, ContextConfiguration.class, ContextHierarchy.class) == null) {
		return buildDefaultMergedContextConfiguration(testClass, cacheAwareContextLoaderDelegate);
	}

So, the question is: is it a desired behavior for @SpringBootTest (which is not a meta-annotation of @ContextConfiguration), or it's a bug?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: waiting-for-triageAn issue we've not yet triaged or decided on

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions