Skip to content

@ActiveProfiles with same profiles but different order results in duplicate ApplicationContext #25973

@sbrannen

Description

@sbrannen

Overview

While working on #25800, I realized that two @ActiveProfiles declarations on different test classes using the same profiles but declared in a different order would result in the creation of a duplicate yet identical ApplicationContext in the context cache in the Spring TestContext Framework. This is because ActiveProfilesUtils stores the active profiles in a LinkedHashSet to preserve registration order. The set is then converted to a String[] which is handed off to MergedContextConfiguration which also uses a LinkedHashSet to ensure uniqueness but still retains the registration order of the active profiles. When the MergedContextConfiguration is used as the context cache key to check whether an ApplicationContext already exists for the given metadata, the implementation of equals() in MergedContextConfiguration uses Arrays.equals() to compare two sets of active profiles, and that results in a cache miss for semantically identical active profile arrays { apples, oranges } and { oranges, apples }. Consequently, an identical ApplicationContext is created and stored in the context cache when that should not be the case.

Deliverables

  • Ensure that active profiles are both unique and sorted within MergedContextConfiguration in order to avoid context cache misses.

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: backportedAn issue that has been backported to maintenance branchestype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions