Skip to content

Context cache is broken if two Bean Override fields declare annotations in different order #33633

@sbrannen

Description

@sbrannen

Given the following example test classes, one would expect that Test1 and Test2 would share the same ApplicationContext.

However, due to the current implementation of equals() in OverrideMetadata, the annotations on the logically equivalent messageService fields are compared using Arrays.equals() which takes the declaration order into account. Whereas, the declaration order of the annotations is not relevant for this particular "equality" check.

@SpringJUnitConfig(Config.class)
class Test1 {

   @TestBean
   @Qualifier("system")
   MessageService messageService;

   // rest of test class
}
@SpringJUnitConfig(Config.class)
class Test2 {

   @Qualifier("system")
   @TestBean
   MessageService messageService;

   // rest of test class
}

In light of the above, we should compare the annotations declared on a Bean Override field using Set semantics.

FYI: Spring Boot's testing support took the same approach in its QualifierDefinition.

Metadata

Metadata

Assignees

Labels

in: testIssues in the test moduletype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions