Skip to content

Commit 930980e

Browse files
committed
Add self reference to ApplicationContextAssertProvider
Signed-off-by: Stefano Cordio <stefano.cordio@gmail.com>
1 parent 3358042 commit 930980e

12 files changed

Lines changed: 65 additions & 41 deletions

File tree

core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import static org.assertj.core.api.Assertions.assertThat;
4343

4444
/**
45-
* AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to an
45+
* AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied to an
4646
* {@link ApplicationContext}.
4747
*
4848
* @param <C> the application context type

core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,18 @@
4444
* Any {@link ApplicationContext} method called on a context that has failed to start will
4545
* throw an {@link IllegalStateException}.
4646
*
47-
* @param <C> the application context type
47+
* @param <SELF> the self class reference
48+
* @param <C> the source application context type
4849
* @author Phillip Webb
50+
* @author Stefano Cordio
4951
* @since 2.0.0
5052
* @see AssertableApplicationContext
5153
* @see AssertableWebApplicationContext
5254
* @see AssertableReactiveWebApplicationContext
5355
* @see ApplicationContextAssert
5456
*/
55-
public interface ApplicationContextAssertProvider<C extends ApplicationContext>
56-
extends ApplicationContext, AssertProvider<ApplicationContextAssert<C>>, Closeable {
57+
public interface ApplicationContextAssertProvider<SELF extends ApplicationContextAssertProvider<SELF, C>, C extends ApplicationContext>
58+
extends ApplicationContext, AssertProvider<ApplicationContextAssert<SELF>>, Closeable {
5759

5860
/**
5961
* Return an assert for AssertJ.
@@ -63,7 +65,7 @@ public interface ApplicationContextAssertProvider<C extends ApplicationContext>
6365
*/
6466
@Deprecated(since = "2.0.0", forRemoval = false)
6567
@Override
66-
ApplicationContextAssert<C> assertThat();
68+
ApplicationContextAssert<SELF> assertThat();
6769

6870
/**
6971
* Return the original source {@link ApplicationContext}.
@@ -104,7 +106,7 @@ public interface ApplicationContextAssertProvider<C extends ApplicationContext>
104106
* {@link ApplicationContext} or throw an exception if the context fails to start.
105107
* @return a {@link ApplicationContextAssertProvider} instance
106108
*/
107-
static <T extends ApplicationContextAssertProvider<C>, C extends ApplicationContext> T get(Class<T> type,
109+
static <T extends ApplicationContextAssertProvider<? super T, C>, C extends ApplicationContext> T get(Class<T> type,
108110
Class<? extends C> contextType, Supplier<? extends C> contextSupplier) {
109111
return get(type, contextType, contextSupplier, new Class<?>[0]);
110112
}
@@ -125,7 +127,7 @@ static <T extends ApplicationContextAssertProvider<C>, C extends ApplicationCont
125127
* @since 3.4.0
126128
*/
127129
@SuppressWarnings("unchecked")
128-
static <T extends ApplicationContextAssertProvider<C>, C extends ApplicationContext> T get(Class<T> type,
130+
static <T extends ApplicationContextAssertProvider<? super T, C>, C extends ApplicationContext> T get(Class<T> type,
129131
Class<? extends C> contextType, Supplier<? extends C> contextSupplier,
130132
Class<?>... additionalContextInterfaces) {
131133
Assert.notNull(type, "'type' must not be null");

core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableApplicationContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
* @see ApplicationContext
3636
*/
3737
public interface AssertableApplicationContext
38-
extends ApplicationContextAssertProvider<ConfigurableApplicationContext>, ConfigurableApplicationContext {
38+
extends ApplicationContextAssertProvider<AssertableApplicationContext, ConfigurableApplicationContext>,
39+
ConfigurableApplicationContext {
3940

4041
/**
4142
* Factory method to create a new {@link AssertableApplicationContext} instance.

core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
* @see ReactiveWebApplicationContext
3434
* @see ReactiveWebApplicationContext
3535
*/
36-
public interface AssertableReactiveWebApplicationContext
37-
extends ApplicationContextAssertProvider<ConfigurableReactiveWebApplicationContext>,
36+
public interface AssertableReactiveWebApplicationContext extends
37+
ApplicationContextAssertProvider<AssertableReactiveWebApplicationContext, ConfigurableReactiveWebApplicationContext>,
3838
ConfigurableReactiveWebApplicationContext {
3939

4040
/**

core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
* @see WebApplicationContext
3636
*/
3737
public interface AssertableWebApplicationContext
38-
extends ApplicationContextAssertProvider<ConfigurableWebApplicationContext>, ConfigurableWebApplicationContext {
38+
extends ApplicationContextAssertProvider<AssertableWebApplicationContext, ConfigurableWebApplicationContext>,
39+
ConfigurableWebApplicationContext {
3940

4041
/**
4142
* Factory method to create a new {@link AssertableWebApplicationContext} instance.

core/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
* @see ReactiveWebApplicationContextRunner
111111
* @see ApplicationContextAssert
112112
*/
113-
public abstract class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<C>> {
113+
public abstract class AbstractApplicationContextRunner<SELF extends AbstractApplicationContextRunner<SELF, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<A, C>> {
114114

115115
private final RunnerConfiguration<C> runnerConfiguration;
116116

core/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.junit.jupiter.api.BeforeEach;
2222
import org.junit.jupiter.api.Test;
23-
import org.mockito.Mock;
2423

2524
import org.springframework.context.ApplicationContext;
2625
import org.springframework.context.ConfigurableApplicationContext;
@@ -40,7 +39,6 @@
4039
*/
4140
class ApplicationContextAssertProviderTests {
4241

43-
@Mock
4442
private final ConfigurableApplicationContext mockContext = mock();
4543

4644
private RuntimeException startupFailure;
@@ -102,15 +100,17 @@ void getWhenSupplierIsNullShouldThrowException() {
102100

103101
@Test
104102
void getWhenContextStartsShouldReturnProxyThatCallsRealMethods() {
105-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
103+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
104+
this.mockContextSupplier);
106105
assertThat((Object) context).isNotNull();
107106
context.getBean("foo");
108107
then(this.mockContext).should().getBean("foo");
109108
}
110109

111110
@Test
112111
void getWhenContextFailsShouldReturnProxyThatThrowsExceptions() {
113-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.startupFailureSupplier);
112+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
113+
this.startupFailureSupplier);
114114
assertThat((Object) context).isNotNull();
115115
assertThatIllegalStateException().isThrownBy(() -> context.getBean("foo"))
116116
.withCause(this.startupFailure)
@@ -119,27 +119,31 @@ void getWhenContextFailsShouldReturnProxyThatThrowsExceptions() {
119119

120120
@Test
121121
void getSourceContextWhenContextStartsShouldReturnSourceContext() {
122-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
122+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
123+
this.mockContextSupplier);
123124
assertThat(context.getSourceApplicationContext()).isSameAs(this.mockContext);
124125
}
125126

126127
@Test
127128
void getSourceContextWhenContextFailsShouldThrowException() {
128-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.startupFailureSupplier);
129+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
130+
this.startupFailureSupplier);
129131
assertThatIllegalStateException().isThrownBy(context::getSourceApplicationContext)
130132
.withCause(this.startupFailure)
131133
.withMessageContaining("failed to start");
132134
}
133135

134136
@Test
135137
void getSourceContextOfTypeWhenContextStartsShouldReturnSourceContext() {
136-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
138+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
139+
this.mockContextSupplier);
137140
assertThat(context.getSourceApplicationContext(ApplicationContext.class)).isSameAs(this.mockContext);
138141
}
139142

140143
@Test
141144
void getSourceContextOfTypeWhenContextFailsToStartShouldThrowException() {
142-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.startupFailureSupplier);
145+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
146+
this.startupFailureSupplier);
143147
assertThatIllegalStateException()
144148
.isThrownBy(() -> context.getSourceApplicationContext(ApplicationContext.class))
145149
.withCause(this.startupFailure)
@@ -148,59 +152,68 @@ void getSourceContextOfTypeWhenContextFailsToStartShouldThrowException() {
148152

149153
@Test
150154
void getStartupFailureWhenContextStartsShouldReturnNull() {
151-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
155+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
156+
this.mockContextSupplier);
152157
assertThat(context.getStartupFailure()).isNull();
153158
}
154159

155160
@Test
156161
void getStartupFailureWhenContextFailsToStartShouldReturnException() {
157-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.startupFailureSupplier);
162+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
163+
this.startupFailureSupplier);
158164
assertThat(context.getStartupFailure()).isEqualTo(this.startupFailure);
159165
}
160166

161167
@Test
162168
void assertThatWhenContextStartsShouldReturnAssertions() {
163-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
164-
ApplicationContextAssert<ApplicationContext> contextAssert = assertThat(context);
169+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
170+
this.mockContextSupplier);
171+
ApplicationContextAssert<TestAssertProviderApplicationContext> contextAssert = assertThat(context);
165172
assertThat(contextAssert.getApplicationContext()).isSameAs(context);
166173
assertThat(contextAssert.getStartupFailure()).isNull();
167174
}
168175

169176
@Test
170177
void assertThatWhenContextFailsShouldReturnAssertions() {
171-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.startupFailureSupplier);
172-
ApplicationContextAssert<ApplicationContext> contextAssert = assertThat(context);
178+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
179+
this.startupFailureSupplier);
180+
ApplicationContextAssert<TestAssertProviderApplicationContext> contextAssert = assertThat(context);
173181
assertThat(contextAssert.getApplicationContext()).isSameAs(context);
174182
assertThat(contextAssert.getStartupFailure()).isSameAs(this.startupFailure);
175183
}
176184

177185
@Test
178186
void toStringWhenContextStartsShouldReturnSimpleString() {
179-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
187+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
188+
this.mockContextSupplier);
180189
assertThat(context.toString()).startsWith("Started application [ConfigurableApplicationContext.MockitoMock")
181190
.endsWith("id = [null], applicationName = [null], beanDefinitionCount = 0]");
182191
}
183192

184193
@Test
185194
void toStringWhenContextFailsToStartShouldReturnSimpleString() {
186-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.startupFailureSupplier);
195+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
196+
this.startupFailureSupplier);
187197
assertThat(context).hasToString("Unstarted application context "
188198
+ "org.springframework.context.ApplicationContext[startupFailure=java.lang.RuntimeException]");
189199
}
190200

191201
@Test
192202
void closeShouldCloseContext() {
193-
ApplicationContextAssertProvider<ApplicationContext> context = get(this.mockContextSupplier);
203+
ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> context = get(
204+
this.mockContextSupplier);
194205
context.close();
195206
then(this.mockContext).should().close();
196207
}
197208

198-
private ApplicationContextAssertProvider<ApplicationContext> get(Supplier<ApplicationContext> contextSupplier) {
209+
private ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> get(
210+
Supplier<ApplicationContext> contextSupplier) {
199211
return ApplicationContextAssertProvider.get(TestAssertProviderApplicationContext.class,
200212
ApplicationContext.class, contextSupplier);
201213
}
202214

203-
interface TestAssertProviderApplicationContext extends ApplicationContextAssertProvider<ApplicationContext> {
215+
interface TestAssertProviderApplicationContext
216+
extends ApplicationContextAssertProvider<TestAssertProviderApplicationContext, ApplicationContext> {
204217

205218
}
206219

core/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,15 @@
6262
* @param <A> the assertable context type
6363
* @author Stephane Nicoll
6464
* @author Phillip Webb
65+
* @author Stefano Cordio
6566
*/
66-
abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicationContextRunner<T, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<C>> {
67+
abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicationContextRunner<T, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<A, C>> {
68+
69+
@Test
70+
void runContextShouldWorkWithSatisfiesAssertion() {
71+
get().run((context) -> assertThat(context).satisfies((ctx) -> assertThat(ctx).hasNotFailed(),
72+
(ctx) -> assertThat(ctx).doesNotHaveBean("foo")));
73+
}
6774

6875
@Test
6976
void runWithInitializerShouldInitialize() {
@@ -123,7 +130,7 @@ void runWithSystemPropertiesWhenValueIsNullShouldRemoveProperty() {
123130
}
124131

125132
@Test
126-
void runWithMultiplePropertyValuesShouldAllAllValues() {
133+
void runWithMultiplePropertyValuesShouldHaveAllValues() {
127134
get().withPropertyValues("test.foo=1").withPropertyValues("test.bar=2").run((context) -> {
128135
Environment environment = context.getEnvironment();
129136
assertThat(environment.getProperty("test.foo")).isEqualTo("1");

module/spring-boot-actuator-autoconfigure/src/testFixtures/java/org/springframework/boot/actuate/autoconfigure/integrationtest/AbstractHealthEndpointAdditionalPathIntegrationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* @param <A> the assertions
3838
* @author Madhura Bhave
3939
*/
40-
public abstract class AbstractHealthEndpointAdditionalPathIntegrationTests<T extends AbstractApplicationContextRunner<T, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<C>> {
40+
public abstract class AbstractHealthEndpointAdditionalPathIntegrationTests<T extends AbstractApplicationContextRunner<T, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<A, C>> {
4141

4242
private final T runner;
4343

module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/GrpcServerAutoConfigurationTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class GrpcServerAutoConfigurationTests {
8686

8787
private final ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build();
8888

89-
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
89+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
9090
.withConfiguration(autoConfigurations)
9191
.with(this::noOpLifecycleBeans)
9292
.with(this::serviceBean);
@@ -350,7 +350,7 @@ void nettyServerFactoryAutoConfiguredWithSsl() {
350350
"nettyGrpcServerLifecycle"));
351351
}
352352

353-
private ContextConsumer<? super ApplicationContextAssertProvider<?>> assertThatServerIsConfigured(
353+
private ContextConsumer<? super ApplicationContextAssertProvider<?, ?>> assertThatServerIsConfigured(
354354
Class<?> expectedServerFactoryType, String expectedAddress, String expectedLifecycleBeanName) {
355355
return (context) -> {
356356
assertThat(context).getBean(GrpcServerFactory.class)
@@ -365,12 +365,12 @@ private ContextConsumer<? super ApplicationContextAssertProvider<?>> assertThatS
365365
};
366366
}
367367

368-
private <R extends AbstractApplicationContextRunner<R, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<C>> R serviceBean(
368+
private <R extends AbstractApplicationContextRunner<R, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<A, C>> R serviceBean(
369369
R contextRunner) {
370370
return contextRunner.withBean(BindableService.class, () -> this.service);
371371
}
372372

373-
private <R extends AbstractApplicationContextRunner<R, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<C>> R noOpLifecycleBeans(
373+
private <R extends AbstractApplicationContextRunner<R, C, A>, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider<A, C>> R noOpLifecycleBeans(
374374
R contextRunner) {
375375
return contextRunner.withBean("shadedNettyGrpcServerLifecycle", GrpcServerLifecycle.class, Mockito::mock)
376376
.withBean("nettyGrpcServerLifecycle", GrpcServerLifecycle.class, Mockito::mock)

0 commit comments

Comments
 (0)