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 6f5be5b15ae1..f39319f6d757 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 @@ -26,7 +26,6 @@ import org.assertj.core.api.AbstractObjectArrayAssert; import org.assertj.core.api.AbstractObjectAssert; import org.assertj.core.api.AbstractThrowableAssert; -import org.assertj.core.api.Assertions; import org.assertj.core.api.MapAssert; import org.assertj.core.error.BasicErrorMessageFactory; import org.jspecify.annotations.Nullable; @@ -37,6 +36,7 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.lang.CheckReturnValue; import org.springframework.util.Assert; import static org.assertj.core.api.Assertions.assertThat; @@ -224,13 +224,14 @@ public ApplicationContextAssert doesNotHaveBean(String name) { * @return array assertions for the bean names * @throws AssertionError if the application context did not start */ + @CheckReturnValue public AbstractObjectArrayAssert getBeanNames(Class type) { if (this.startupFailure != null) { throwAssertionError(contextFailedToStartWhenExpecting(this.startupFailure, "to get beans names with type:%n <%s>", type)); } - return Assertions.assertThat(getApplicationContext().getBeanNamesForType(type)) - .as("Bean names of type <%s> from <%s>", type, getApplicationContext()); + return assertThat(getApplicationContext().getBeanNamesForType(type)).as("Bean names of type <%s> from <%s>", + type, getApplicationContext()); } /** @@ -249,6 +250,7 @@ public AbstractObjectArrayAssert getBeanNames(Class type) { * @throws AssertionError if the application context contains multiple beans of the * given type */ + @CheckReturnValue public AbstractObjectAssert getBean(Class type) { return getBean(type, Scope.INCLUDE_ANCESTORS); } @@ -270,6 +272,7 @@ public AbstractObjectAssert getBean(Class type) { * @throws AssertionError if the application context contains multiple beans of the * given type */ + @CheckReturnValue public AbstractObjectAssert getBean(Class type, Scope scope) { Assert.notNull(scope, "'scope' must not be null"); if (this.startupFailure != null) { @@ -284,7 +287,7 @@ public AbstractObjectAssert getBean(Class type, Scope scope) { getApplicationContext(), type, names)); } T bean = (name != null) ? getApplicationContext().getBean(name, type) : null; - return Assertions.assertThat(bean).as("Bean of type <%s> from <%s>", type, getApplicationContext()); + return assertThat(bean).as("Bean of type <%s> from <%s>", type, getApplicationContext()); } private @Nullable String getPrimary(String[] names, Scope scope) { @@ -330,13 +333,14 @@ private boolean isPrimary(String name, Scope scope) { * is found * @throws AssertionError if the application context did not start */ + @CheckReturnValue public AbstractObjectAssert getBean(String name) { if (this.startupFailure != null) { throwAssertionError( contextFailedToStartWhenExpecting(this.startupFailure, "to contain a bean of name:%n <%s>", name)); } Object bean = findBean(name); - return Assertions.assertThat(bean).as("Bean of name <%s> from <%s>", name, getApplicationContext()); + return assertThat(bean).as("Bean of name <%s> from <%s>", name, getApplicationContext()); } /** @@ -357,6 +361,7 @@ public AbstractObjectAssert getBean(String name) { * name but a different type */ @SuppressWarnings("unchecked") + @CheckReturnValue public AbstractObjectAssert getBean(String name, Class type) { if (this.startupFailure != null) { throwAssertionError(contextFailedToStartWhenExpecting(this.startupFailure, @@ -368,8 +373,8 @@ public AbstractObjectAssert getBean(String name, Class type) { "%nExpecting:%n <%s>%nto contain a bean of name:%n <%s> (%s)%nbut found:%n <%s> of type <%s>", getApplicationContext(), name, type, bean, bean.getClass())); } - return Assertions.assertThat((T) bean) - .as("Bean of name <%s> and type <%s> from <%s>", name, type, getApplicationContext()); + return assertThat((T) bean).as("Bean of name <%s> and type <%s> from <%s>", name, type, + getApplicationContext()); } private @Nullable Object findBean(String name) { @@ -395,6 +400,7 @@ public AbstractObjectAssert getBean(String name, Class type) { * no beans are found * @throws AssertionError if the application context did not start */ + @CheckReturnValue public MapAssert getBeans(Class type) { return getBeans(type, Scope.INCLUDE_ANCESTORS); } @@ -414,14 +420,15 @@ public MapAssert getBeans(Class type) { * no beans are found * @throws AssertionError if the application context did not start */ + @CheckReturnValue public MapAssert getBeans(Class type, Scope scope) { Assert.notNull(scope, "'scope' must not be null"); if (this.startupFailure != null) { throwAssertionError( contextFailedToStartWhenExpecting(this.startupFailure, "to get beans of type:%n <%s>", type)); } - return Assertions.assertThat(scope.getBeansOfType(getApplicationContext(), type)) - .as("Beans of type <%s> from <%s>", type, getApplicationContext()); + return assertThat(scope.getBeansOfType(getApplicationContext(), type)).as("Beans of type <%s> from <%s>", type, + getApplicationContext()); } /** @@ -434,6 +441,7 @@ public MapAssert getBeans(Class type, Scope scope) { * @return assertions on the cause of the failure * @throws AssertionError if the application context started without a failure */ + @CheckReturnValue public AbstractThrowableAssert getFailure() { hasFailed(); return assertThat(this.startupFailure);