diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java index b1dbbfa04596..4b3880e56831 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssert.java @@ -42,7 +42,7 @@ import static org.assertj.core.api.Assertions.assertThat; /** - * AssertJ {@link org.assertj.core.api.Assert assertions} that can be applied to an + * AssertJ {@linkplain org.assertj.core.api.Assert assertions} that can be applied to an * {@link ApplicationContext}. * * @param the application context type diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java index 1db7a364f8fd..bae56ad3034f 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProvider.java @@ -44,16 +44,18 @@ * Any {@link ApplicationContext} method called on a context that has failed to start will * throw an {@link IllegalStateException}. * - * @param the application context type + * @param the self class reference + * @param the source application context type * @author Phillip Webb + * @author Stefano Cordio * @since 2.0.0 * @see AssertableApplicationContext * @see AssertableWebApplicationContext * @see AssertableReactiveWebApplicationContext * @see ApplicationContextAssert */ -public interface ApplicationContextAssertProvider - extends ApplicationContext, AssertProvider>, Closeable { +public interface ApplicationContextAssertProvider, C extends ApplicationContext> + extends ApplicationContext, AssertProvider>, Closeable { /** * Return an assert for AssertJ. @@ -63,7 +65,7 @@ public interface ApplicationContextAssertProvider */ @Deprecated(since = "2.0.0", forRemoval = false) @Override - ApplicationContextAssert assertThat(); + ApplicationContextAssert assertThat(); /** * Return the original source {@link ApplicationContext}. @@ -104,7 +106,7 @@ public interface ApplicationContextAssertProvider * {@link ApplicationContext} or throw an exception if the context fails to start. * @return a {@link ApplicationContextAssertProvider} instance */ - static , C extends ApplicationContext> T get(Class type, + static , C extends ApplicationContext> T get(Class type, Class contextType, Supplier contextSupplier) { return get(type, contextType, contextSupplier, new Class[0]); } @@ -125,7 +127,7 @@ static , C extends ApplicationCont * @since 3.4.0 */ @SuppressWarnings("unchecked") - static , C extends ApplicationContext> T get(Class type, + static , C extends ApplicationContext> T get(Class type, Class contextType, Supplier contextSupplier, Class... additionalContextInterfaces) { Assert.notNull(type, "'type' must not be null"); diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableApplicationContext.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableApplicationContext.java index db4cbc32f97a..4a79387d005d 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableApplicationContext.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableApplicationContext.java @@ -35,7 +35,8 @@ * @see ApplicationContext */ public interface AssertableApplicationContext - extends ApplicationContextAssertProvider, ConfigurableApplicationContext { + extends ApplicationContextAssertProvider, + ConfigurableApplicationContext { /** * Factory method to create a new {@link AssertableApplicationContext} instance. diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContext.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContext.java index 78a349d0ff86..a6fe90e87cd1 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContext.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableReactiveWebApplicationContext.java @@ -33,8 +33,8 @@ * @see ReactiveWebApplicationContext * @see ReactiveWebApplicationContext */ -public interface AssertableReactiveWebApplicationContext - extends ApplicationContextAssertProvider, +public interface AssertableReactiveWebApplicationContext extends + ApplicationContextAssertProvider, ConfigurableReactiveWebApplicationContext { /** diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContext.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContext.java index ba61598ec6b7..1a6094d256d6 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContext.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/assertj/AssertableWebApplicationContext.java @@ -35,7 +35,8 @@ * @see WebApplicationContext */ public interface AssertableWebApplicationContext - extends ApplicationContextAssertProvider, ConfigurableWebApplicationContext { + extends ApplicationContextAssertProvider, + ConfigurableWebApplicationContext { /** * Factory method to create a new {@link AssertableWebApplicationContext} instance. diff --git a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java index 7afc3a65ece9..9b9477078c98 100644 --- a/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java +++ b/core/spring-boot-test/src/main/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunner.java @@ -110,7 +110,7 @@ * @see ReactiveWebApplicationContextRunner * @see ApplicationContextAssert */ -public abstract class AbstractApplicationContextRunner, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> { +public abstract class AbstractApplicationContextRunner, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> { private final RunnerConfiguration runnerConfiguration; diff --git a/core/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java b/core/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java index f45941c26b40..7b9ebece7746 100644 --- a/core/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java +++ b/core/spring-boot-test/src/test/java/org/springframework/boot/test/context/assertj/ApplicationContextAssertProviderTests.java @@ -20,7 +20,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mock; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; @@ -40,7 +39,6 @@ */ class ApplicationContextAssertProviderTests { - @Mock private final ConfigurableApplicationContext mockContext = mock(); private RuntimeException startupFailure; @@ -102,7 +100,8 @@ void getWhenSupplierIsNullShouldThrowException() { @Test void getWhenContextStartsShouldReturnProxyThatCallsRealMethods() { - ApplicationContextAssertProvider context = get(this.mockContextSupplier); + ApplicationContextAssertProvider context = get( + this.mockContextSupplier); assertThat((Object) context).isNotNull(); context.getBean("foo"); then(this.mockContext).should().getBean("foo"); @@ -110,7 +109,8 @@ void getWhenContextStartsShouldReturnProxyThatCallsRealMethods() { @Test void getWhenContextFailsShouldReturnProxyThatThrowsExceptions() { - ApplicationContextAssertProvider context = get(this.startupFailureSupplier); + ApplicationContextAssertProvider context = get( + this.startupFailureSupplier); assertThat((Object) context).isNotNull(); assertThatIllegalStateException().isThrownBy(() -> context.getBean("foo")) .withCause(this.startupFailure) @@ -119,13 +119,15 @@ void getWhenContextFailsShouldReturnProxyThatThrowsExceptions() { @Test void getSourceContextWhenContextStartsShouldReturnSourceContext() { - ApplicationContextAssertProvider context = get(this.mockContextSupplier); + ApplicationContextAssertProvider context = get( + this.mockContextSupplier); assertThat(context.getSourceApplicationContext()).isSameAs(this.mockContext); } @Test void getSourceContextWhenContextFailsShouldThrowException() { - ApplicationContextAssertProvider context = get(this.startupFailureSupplier); + ApplicationContextAssertProvider context = get( + this.startupFailureSupplier); assertThatIllegalStateException().isThrownBy(context::getSourceApplicationContext) .withCause(this.startupFailure) .withMessageContaining("failed to start"); @@ -133,13 +135,15 @@ void getSourceContextWhenContextFailsShouldThrowException() { @Test void getSourceContextOfTypeWhenContextStartsShouldReturnSourceContext() { - ApplicationContextAssertProvider context = get(this.mockContextSupplier); + ApplicationContextAssertProvider context = get( + this.mockContextSupplier); assertThat(context.getSourceApplicationContext(ApplicationContext.class)).isSameAs(this.mockContext); } @Test void getSourceContextOfTypeWhenContextFailsToStartShouldThrowException() { - ApplicationContextAssertProvider context = get(this.startupFailureSupplier); + ApplicationContextAssertProvider context = get( + this.startupFailureSupplier); assertThatIllegalStateException() .isThrownBy(() -> context.getSourceApplicationContext(ApplicationContext.class)) .withCause(this.startupFailure) @@ -148,59 +152,68 @@ void getSourceContextOfTypeWhenContextFailsToStartShouldThrowException() { @Test void getStartupFailureWhenContextStartsShouldReturnNull() { - ApplicationContextAssertProvider context = get(this.mockContextSupplier); + ApplicationContextAssertProvider context = get( + this.mockContextSupplier); assertThat(context.getStartupFailure()).isNull(); } @Test void getStartupFailureWhenContextFailsToStartShouldReturnException() { - ApplicationContextAssertProvider context = get(this.startupFailureSupplier); + ApplicationContextAssertProvider context = get( + this.startupFailureSupplier); assertThat(context.getStartupFailure()).isEqualTo(this.startupFailure); } @Test void assertThatWhenContextStartsShouldReturnAssertions() { - ApplicationContextAssertProvider context = get(this.mockContextSupplier); - ApplicationContextAssert contextAssert = assertThat(context); + ApplicationContextAssertProvider context = get( + this.mockContextSupplier); + ApplicationContextAssert contextAssert = assertThat(context); assertThat(contextAssert.getApplicationContext()).isSameAs(context); assertThat(contextAssert.getStartupFailure()).isNull(); } @Test void assertThatWhenContextFailsShouldReturnAssertions() { - ApplicationContextAssertProvider context = get(this.startupFailureSupplier); - ApplicationContextAssert contextAssert = assertThat(context); + ApplicationContextAssertProvider context = get( + this.startupFailureSupplier); + ApplicationContextAssert contextAssert = assertThat(context); assertThat(contextAssert.getApplicationContext()).isSameAs(context); assertThat(contextAssert.getStartupFailure()).isSameAs(this.startupFailure); } @Test void toStringWhenContextStartsShouldReturnSimpleString() { - ApplicationContextAssertProvider context = get(this.mockContextSupplier); + ApplicationContextAssertProvider context = get( + this.mockContextSupplier); assertThat(context.toString()).startsWith("Started application [ConfigurableApplicationContext.MockitoMock") .endsWith("id = [null], applicationName = [null], beanDefinitionCount = 0]"); } @Test void toStringWhenContextFailsToStartShouldReturnSimpleString() { - ApplicationContextAssertProvider context = get(this.startupFailureSupplier); + ApplicationContextAssertProvider context = get( + this.startupFailureSupplier); assertThat(context).hasToString("Unstarted application context " + "org.springframework.context.ApplicationContext[startupFailure=java.lang.RuntimeException]"); } @Test void closeShouldCloseContext() { - ApplicationContextAssertProvider context = get(this.mockContextSupplier); + ApplicationContextAssertProvider context = get( + this.mockContextSupplier); context.close(); then(this.mockContext).should().close(); } - private ApplicationContextAssertProvider get(Supplier contextSupplier) { + private ApplicationContextAssertProvider get( + Supplier contextSupplier) { return ApplicationContextAssertProvider.get(TestAssertProviderApplicationContext.class, ApplicationContext.class, contextSupplier); } - interface TestAssertProviderApplicationContext extends ApplicationContextAssertProvider { + interface TestAssertProviderApplicationContext + extends ApplicationContextAssertProvider { } diff --git a/core/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java b/core/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java index 43fcdf7f74b3..755e43e29961 100644 --- a/core/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java +++ b/core/spring-boot-test/src/test/java/org/springframework/boot/test/context/runner/AbstractApplicationContextRunnerTests.java @@ -62,8 +62,15 @@ * @param the assertable context type * @author Stephane Nicoll * @author Phillip Webb + * @author Stefano Cordio */ -abstract class AbstractApplicationContextRunnerTests, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> { +abstract class AbstractApplicationContextRunnerTests, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> { + + @Test + void runContextShouldWorkWithSatisfiesAssertion() { + get().run((context) -> assertThat(context).satisfies((ctx) -> assertThat(ctx).hasNotFailed(), + (ctx) -> assertThat(ctx).doesNotHaveBean("foo"))); + } @Test void runWithInitializerShouldInitialize() { @@ -123,7 +130,7 @@ void runWithSystemPropertiesWhenValueIsNullShouldRemoveProperty() { } @Test - void runWithMultiplePropertyValuesShouldAllAllValues() { + void runWithMultiplePropertyValuesShouldHaveAllValues() { get().withPropertyValues("test.foo=1").withPropertyValues("test.bar=2").run((context) -> { Environment environment = context.getEnvironment(); assertThat(environment.getProperty("test.foo")).isEqualTo("1"); diff --git a/module/spring-boot-actuator-autoconfigure/src/testFixtures/java/org/springframework/boot/actuate/autoconfigure/integrationtest/AbstractHealthEndpointAdditionalPathIntegrationTests.java b/module/spring-boot-actuator-autoconfigure/src/testFixtures/java/org/springframework/boot/actuate/autoconfigure/integrationtest/AbstractHealthEndpointAdditionalPathIntegrationTests.java index c1efa3e75a19..9d67e2422a51 100644 --- a/module/spring-boot-actuator-autoconfigure/src/testFixtures/java/org/springframework/boot/actuate/autoconfigure/integrationtest/AbstractHealthEndpointAdditionalPathIntegrationTests.java +++ b/module/spring-boot-actuator-autoconfigure/src/testFixtures/java/org/springframework/boot/actuate/autoconfigure/integrationtest/AbstractHealthEndpointAdditionalPathIntegrationTests.java @@ -37,7 +37,7 @@ * @param the assertions * @author Madhura Bhave */ -public abstract class AbstractHealthEndpointAdditionalPathIntegrationTests, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> { +public abstract class AbstractHealthEndpointAdditionalPathIntegrationTests, C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> { private final T runner; diff --git a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/GrpcServerAutoConfigurationTests.java b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/GrpcServerAutoConfigurationTests.java index f0721b1e2fe3..305c564e4f74 100644 --- a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/GrpcServerAutoConfigurationTests.java +++ b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/GrpcServerAutoConfigurationTests.java @@ -86,7 +86,7 @@ class GrpcServerAutoConfigurationTests { private final ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build(); - private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(autoConfigurations) .with(this::noOpLifecycleBeans) .with(this::serviceBean); @@ -350,7 +350,7 @@ void nettyServerFactoryAutoConfiguredWithSsl() { "nettyGrpcServerLifecycle")); } - private ContextConsumer> assertThatServerIsConfigured( + private ContextConsumer> assertThatServerIsConfigured( Class expectedServerFactoryType, String expectedAddress, String expectedLifecycleBeanName) { return (context) -> { assertThat(context).getBean(GrpcServerFactory.class) @@ -365,12 +365,12 @@ private ContextConsumer> assertThatS }; } - private , C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> R serviceBean( + private , C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> R serviceBean( R contextRunner) { return contextRunner.withBean(BindableService.class, () -> this.service); } - private , C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> R noOpLifecycleBeans( + private , C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> R noOpLifecycleBeans( R contextRunner) { return contextRunner.withBean("shadedNettyGrpcServerLifecycle", GrpcServerLifecycle.class, Mockito::mock) .withBean("nettyGrpcServerLifecycle", GrpcServerLifecycle.class, Mockito::mock) diff --git a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/GrpcServerHealthAutoConfigurationTests.java b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/GrpcServerHealthAutoConfigurationTests.java index 527c284f61ca..ebd3e8ff439c 100644 --- a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/GrpcServerHealthAutoConfigurationTests.java +++ b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/health/GrpcServerHealthAutoConfigurationTests.java @@ -348,7 +348,7 @@ private void assertNotConfigured(AssertableApplicationContext context) { assertThat(context).doesNotHaveBean(GrpcServerHealthAutoConfiguration.class); } - private , C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> R serviceBean( + private , C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> R serviceBean( R contextRunner) { return contextRunner.withBean(BindableService.class, () -> this.service); } diff --git a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/security/GrpcServerOAuth2ResourceServerAutoConfigurationTests.java b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/security/GrpcServerOAuth2ResourceServerAutoConfigurationTests.java index f20be2bf4ed7..202d66a2ae05 100644 --- a/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/security/GrpcServerOAuth2ResourceServerAutoConfigurationTests.java +++ b/module/spring-boot-grpc-server/src/test/java/org/springframework/boot/grpc/server/autoconfigure/security/GrpcServerOAuth2ResourceServerAutoConfigurationTests.java @@ -62,7 +62,7 @@ class GrpcServerOAuth2ResourceServerAutoConfigurationTests { private static final AutoConfigurations autoConfigurations = AutoConfigurations .of(OAuth2ResourceServerAutoConfiguration.class, GrpcServerOAuth2ResourceServerAutoConfiguration.class); - private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(autoConfigurations) .withUserConfiguration(GrpcSecurityConfiguration.class) .with(this::serviceBean) @@ -115,7 +115,7 @@ void notConfiguredInWebApplicationWithNoBindableService() { .run((context) -> assertThat(context).doesNotHaveBean(AuthenticationProcessInterceptor.class)); } - private , C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> R serviceBean( + private , C extends ConfigurableApplicationContext, A extends ApplicationContextAssertProvider> R serviceBean( R contextRunner) { BindableService service = mock(); ServerServiceDefinition serviceDefinition = ServerServiceDefinition.builder("my-service").build();