Skip to content

Improve testability of new Profiles support [SPR-16962] #21500

@spring-projects-issues

Description

@spring-projects-issues

Andy Wilkinson opened SPR-16962 and commented

The introduction of the Profiles type in #17063 has made verifying that a call to Environment.acceptsProfiles has been made more difficult. When String was used, verification with Mockito was easy:

verify(this.environment).acceptsProfiles("dev");

With the new Profiles type this will fail as ParsedProfiles does not override equals:

verify(this.environment).acceptsProfiles(Profiles.of("dev"));

The best that I have come up with is to capture the argument that's passed into acceptsProfiles, abuse matches to capture the names of the profiles, and assert that they are as expected:

ArgumentCaptor<Profiles> profiles = ArgumentCaptor.forClass(Profiles.class);
verify(this.environment).acceptsProfiles(profiles.capture());
List<String> profileNames = new ArrayList<String>();
profiles.getValue().matches((profile) -> {
	profileNames.add(profile);
	return false;
});
assertThat(profileNames).containsExactly("dev");

Affects: 5.1 RC1

Issue Links:

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: duplicateA duplicate of another issuetype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions