From a5e6f1bf7bc1590e72ab7f4a2b9772fb3715ef4a Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 6 Oct 2026 20:27:07 +0200 Subject: [PATCH] Fix assertions whose result was ignored Several tests called AssertJ's assertThat without chaining an assertion, or chained withFailMessage after the assertion had already run, so they verified nothing. Found with OpenRewrite's FindIgnoredCheckReturnValue. See gh-52045 Signed-off-by: Tim te Beek --- .../boot/cli/command/init/InitCommandTests.java | 6 ++++-- .../boot/autoconfigure/jmx/JmxAutoConfigurationTests.java | 4 ++-- .../loader/zip/Zip64EndOfCentralDirectoryRecordTests.java | 2 +- .../ElasticsearchRestClientAutoConfigurationTests.java | 8 ++++---- .../RSocketServerAutoConfigurationTests.java | 2 +- .../Saml2RelyingPartyAutoConfigurationTests.java | 3 +-- .../AbstractReactiveWebServerAutoConfigurationTests.java | 2 +- 7 files changed, 14 insertions(+), 13 deletions(-) diff --git a/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java b/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java index 53ad18de35f..c198632e30f 100644 --- a/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java +++ b/cli/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/init/InitCommandTests.java @@ -409,8 +409,10 @@ class InitCommandTests extends AbstractHttpClientMockTests { void userAgent() throws Exception { this.command.run("--list", "--target=https://fake-service"); then(this.http).should() - .executeOpen(any(HttpHost.class), assertArg((request) -> assertThat( - request.getHeaders("User-Agent")[0].getValue().startsWith("SpringBootCli/"))), isNull()); + .executeOpen(any(HttpHost.class), + assertArg((request) -> assertThat(request.getHeaders("User-Agent")[0].getValue()) + .startsWith("SpringBootCli/")), + isNull()); } private void withUserDir(File userDir, ThrowingRunnable action) throws Exception { diff --git a/core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java b/core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java index f832726a6de..c4f690c5465 100644 --- a/core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java +++ b/core/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jmx/JmxAutoConfigurationTests.java @@ -102,7 +102,7 @@ class JmxAutoConfigurationTests { try (AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext()) { parent.register(JmxAutoConfiguration.class); parent.refresh(); - this.contextRunner.withParent(parent).run((context) -> assertThat(context.isRunning())); + this.contextRunner.withParent(parent).run((context) -> assertThat(context.isRunning()).isTrue()); } } @@ -113,7 +113,7 @@ class JmxAutoConfigurationTests { parent.refresh(); this.contextRunner.withParent(parent) .withConfiguration(UserConfigurations.of(TestConfiguration.class)) - .run((context) -> assertThat(context.isRunning())); + .run((context) -> assertThat(context.isRunning()).isTrue()); } } diff --git a/loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/zip/Zip64EndOfCentralDirectoryRecordTests.java b/loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/zip/Zip64EndOfCentralDirectoryRecordTests.java index 857500cb9e4..0c36514d51a 100644 --- a/loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/zip/Zip64EndOfCentralDirectoryRecordTests.java +++ b/loader/spring-boot-loader/src/test/java/org/springframework/boot/loader/zip/Zip64EndOfCentralDirectoryRecordTests.java @@ -52,7 +52,7 @@ class Zip64EndOfCentralDirectoryRecordTests { assertThat(eocd.numberOfCentralDirectoryEntriesOnThisDisk()).isEqualTo(6); assertThat(eocd.totalNumberOfCentralDirectoryEntries()).isEqualTo(7); assertThat(eocd.sizeOfCentralDirectory()).isEqualTo(8); - assertThat(eocd.offsetToStartOfCentralDirectory()); + assertThat(eocd.offsetToStartOfCentralDirectory()).isEqualTo(9); } @Test diff --git a/module/spring-boot-elasticsearch/src/test/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchRestClientAutoConfigurationTests.java b/module/spring-boot-elasticsearch/src/test/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchRestClientAutoConfigurationTests.java index ff66a78cf30..2014ff56acf 100644 --- a/module/spring-boot-elasticsearch/src/test/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchRestClientAutoConfigurationTests.java +++ b/module/spring-boot-elasticsearch/src/test/java/org/springframework/boot/elasticsearch/autoconfigure/ElasticsearchRestClientAutoConfigurationTests.java @@ -203,8 +203,8 @@ class ElasticsearchRestClientAutoConfigurationTests { Rest5Client client = context.getBean(Rest5Client.class); assertThat(client).extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class)) .satisfiesOnlyOnce((header) -> { - assertThat(header.getName().equals("Authorization")); - assertThat(header.getValue().equals("ApiKey some-api-key")); + assertThat(header.getName()).isEqualTo("Authorization"); + assertThat(header.getValue()).isEqualTo("ApiKey some-api-key"); }); }); } @@ -218,8 +218,8 @@ class ElasticsearchRestClientAutoConfigurationTests { Rest5Client client = context.getBean(Rest5Client.class); assertThat(client).extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class)) .satisfiesOnlyOnce((header) -> { - assertThat(header.getName().equals("Authorization")); - assertThat(header.getValue().equals("ApiKey some-api-key")); + assertThat(header.getName()).isEqualTo("Authorization"); + assertThat(header.getValue()).isEqualTo("ApiKey some-api-key"); }); assertThat(client) .extracting("client.credentialsProvider", InstanceOfAssertFactories.type(CredentialsProvider.class)) diff --git a/module/spring-boot-rsocket/src/test/java/org/springframework/boot/rsocket/autoconfigure/RSocketServerAutoConfigurationTests.java b/module/spring-boot-rsocket/src/test/java/org/springframework/boot/rsocket/autoconfigure/RSocketServerAutoConfigurationTests.java index c20f5067f2f..41b67ca22f8 100644 --- a/module/spring-boot-rsocket/src/test/java/org/springframework/boot/rsocket/autoconfigure/RSocketServerAutoConfigurationTests.java +++ b/module/spring-boot-rsocket/src/test/java/org/springframework/boot/rsocket/autoconfigure/RSocketServerAutoConfigurationTests.java @@ -174,7 +174,7 @@ class RSocketServerAutoConfigurationTests { .run((context) -> { assertThat(context).hasFailed(); assertThat(context.getStartupFailure()).hasRootCauseInstanceOf(NoSuchSslBundleException.class) - .withFailMessage("SSL bundle name 'test-bundle' is not valid"); + .hasRootCauseMessage("SSL bundle name 'test-bundle' cannot be found"); }); } diff --git a/module/spring-boot-security-saml2/src/test/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyAutoConfigurationTests.java b/module/spring-boot-security-saml2/src/test/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyAutoConfigurationTests.java index 2194678c658..f139866ecf5 100644 --- a/module/spring-boot-security-saml2/src/test/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyAutoConfigurationTests.java +++ b/module/spring-boot-security-saml2/src/test/java/org/springframework/boot/security/saml2/autoconfigure/Saml2RelyingPartyAutoConfigurationTests.java @@ -336,8 +336,7 @@ class Saml2RelyingPartyAutoConfigurationTests { void causesManagementWebSecurityAutoConfigurationToBackOff() { WebApplicationContextRunner contextRunner = this.contextRunner.withConfiguration( AutoConfigurations.of(ManagementWebSecurityAutoConfiguration.class, WebMvcAutoConfiguration.class)); - assertThat(contextRunner - .run((context) -> assertThat(context).hasSingleBean(ManagementWebSecurityAutoConfiguration.class))); + contextRunner.run((context) -> assertThat(context).hasSingleBean(ManagementWebSecurityAutoConfiguration.class)); contextRunner.withPropertyValues(PREFIX + ".simplesamlphp.assertingparty.single-sign-on.url=https://simplesaml-for-spring-saml/SSOService.php", PREFIX + ".simplesamlphp.assertingparty.single-sign-on.sign-request=false", diff --git a/module/spring-boot-web-server/src/testFixtures/java/org/springframework/boot/web/server/autoconfigure/reactive/AbstractReactiveWebServerAutoConfigurationTests.java b/module/spring-boot-web-server/src/testFixtures/java/org/springframework/boot/web/server/autoconfigure/reactive/AbstractReactiveWebServerAutoConfigurationTests.java index 57f7e316ce7..83fa99de1c6 100644 --- a/module/spring-boot-web-server/src/testFixtures/java/org/springframework/boot/web/server/autoconfigure/reactive/AbstractReactiveWebServerAutoConfigurationTests.java +++ b/module/spring-boot-web-server/src/testFixtures/java/org/springframework/boot/web/server/autoconfigure/reactive/AbstractReactiveWebServerAutoConfigurationTests.java @@ -108,7 +108,7 @@ public abstract class AbstractReactiveWebServerAutoConfigurationTests { .run((context) -> { assertThat(context).hasFailed(); assertThat(context.getStartupFailure().getCause()).isInstanceOf(NoSuchSslBundleException.class) - .withFailMessage("test"); + .hasMessage("SSL bundle name 'test-bundle' cannot be found"); }); }