Skip to content

Add self reference to ApplicationContextAssertProvider#50500

Open
scordio wants to merge 1 commit into
spring-projects:mainfrom
scordio:ApplicationContextAssertProvider-generics
Open

Add self reference to ApplicationContextAssertProvider#50500
scordio wants to merge 1 commit into
spring-projects:mainfrom
scordio:ApplicationContextAssertProvider-generics

Conversation

@scordio
Copy link
Copy Markdown
Contributor

@scordio scordio commented May 25, 2026

Prior to this change, the following wouldn't compile:

new ApplicationContextRunner().run(context ->
  assertThat(context)            // returns ApplicationContextAssert<ConfigurableApplicationContext>
    .satisfies(
      ctx -> assertThat(ctx)     // returns ObjectAssert<ConfigurableApplicationContext>
        .hasNotFailed(),         // does not compile
      ctx -> assertThat(ctx)     // returns ObjectAssert<ConfigurableApplicationContext>
        .doesNotHaveBean("foo")) // does not compile
    );

This was due to ApplicationContextAssertProvider carrying ConfigurableApplicationContext as the type under assertion, which didn't play well with AssertJ methods like satisfies that allow nesting further assertions via given consumers.

Now ApplicationContextAssertProvider instances carry a self-reference, and assertions like satisfies can be chained correctly:

new ApplicationContextRunner().run(context ->
  assertThat(context)            // returns ApplicationContextAssert<AssertableApplicationContext>
    .satisfies(
      ctx -> assertThat(ctx)     // returns ApplicationContextAssert<AssertableApplicationContext>
        .hasNotFailed(),         // compiles!
      ctx -> assertThat(ctx)     // returns ApplicationContextAssert<AssertableApplicationContext>
        .doesNotHaveBean("foo")) // compiles!
    );

This is effectively a breaking change due to the additional type parameters of ApplicationContextAssertProvider. However, I imagine the change will be transparent to users who use the ApplicationContextRunner via its fluent API.

Relates to:

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 25, 2026
@scordio scordio marked this pull request as draft May 25, 2026 17:06
@scordio scordio force-pushed the ApplicationContextAssertProvider-generics branch 2 times, most recently from 99d0045 to 1922982 Compare May 25, 2026 18:10
Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
@scordio scordio force-pushed the ApplicationContextAssertProvider-generics branch from 1922982 to 930980e Compare May 25, 2026 19:37
@scordio scordio marked this pull request as ready for review May 25, 2026 19:37
@snicoll
Copy link
Copy Markdown
Member

snicoll commented May 26, 2026

Yeah I agree that would be more idiomatic indeed. However, this is a breaking change for anyone implementing the interface so I don't think we can merge this sooner than 4.2.x. Flagging to get more feedback from the team.

@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label May 26, 2026
@philwebb
Copy link
Copy Markdown
Member

My vote is to wait until 4.2.x at this point. It's a nice change, but I don't want to risk accidentally breaking things this late in the release cycle.

@snicoll snicoll added this to the 4.x milestone May 30, 2026
@snicoll snicoll added type: enhancement A general enhancement and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels May 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants