From 92263e7e35bc3c2c049625f8d561716c49d5ca1f Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 6 Jan 2026 12:22:35 -0800 Subject: [PATCH] Add checkstyle rule for AssertJ Assertions import Add a rule to ensure we consistently use `assertThat` rather than `Assertions.assertThat`. See gh-48630 --- .../tracing/BraveAutoConfigurationTests.java | 4 +-- .../WebFluxAutoConfigurationTests.java | 10 +++---- .../DefaultDockerComposeIntegrationTests.java | 6 ++-- .../test/json/AbstractJsonMarshalTester.java | 6 ++-- .../properties/PropertyMapperTests.java | 30 +++++++++++-------- .../AbstractStructuredLoggingTests.java | 4 +-- .../AbstractStructuredLoggingTests.java | 4 +-- src/checkstyle/checkstyle-suppressions.xml | 3 ++ src/checkstyle/checkstyle.xml | 8 +++++ 9 files changed, 44 insertions(+), 31 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java index 5574a41cfa6..d596a309bee 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java @@ -46,7 +46,6 @@ import io.micrometer.tracing.brave.bridge.W3CPropagation; import io.micrometer.tracing.exporter.SpanExportingPredicate; import io.micrometer.tracing.exporter.SpanFilter; import io.micrometer.tracing.exporter.SpanReporter; -import org.assertj.core.api.Assertions; import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; @@ -62,6 +61,7 @@ import org.springframework.core.annotation.Order; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatException; +import static org.assertj.core.api.Assertions.fail; import static org.mockito.Mockito.mock; /** @@ -400,7 +400,7 @@ class BraveAutoConfigurationTests { if (factory instanceof CompositePropagationFactory compositePropagationFactory) { return compositePropagationFactory.getInjectors().toList(); } - Assertions.fail("Expected CompositePropagationFactory, found %s".formatted(factory.getClass())); + fail("Expected CompositePropagationFactory, found %s".formatted(factory.getClass())); throw new AssertionError("Unreachable"); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java index 707963f88b1..ad3d1bede7e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java @@ -36,7 +36,6 @@ import jakarta.validation.ValidatorFactory; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; -import org.assertj.core.api.Assertions; import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.EnabledForJreRange; @@ -122,6 +121,7 @@ import org.springframework.web.server.session.WebSessionStore; import org.springframework.web.util.pattern.PathPattern; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.setExtractBareNamePropertyMethods; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.then; import static org.mockito.Mockito.mock; @@ -475,7 +475,7 @@ class WebFluxAutoConfigurationTests { @Test void cachePeriod() { - Assertions.setExtractBareNamePropertyMethods(false); + setExtractBareNamePropertyMethods(false); this.contextRunner.withPropertyValues("spring.web.resources.cache.period:5").run((context) -> { Map handlerMap = getHandlerMap(context); assertThat(handlerMap).hasSize(2); @@ -486,12 +486,12 @@ class WebFluxAutoConfigurationTests { } } }); - Assertions.setExtractBareNamePropertyMethods(true); + setExtractBareNamePropertyMethods(true); } @Test void cacheControl() { - Assertions.setExtractBareNamePropertyMethods(false); + setExtractBareNamePropertyMethods(false); this.contextRunner .withPropertyValues("spring.web.resources.cache.cachecontrol.max-age:5", "spring.web.resources.cache.cachecontrol.proxy-revalidate:true") @@ -505,7 +505,7 @@ class WebFluxAutoConfigurationTests { } } }); - Assertions.setExtractBareNamePropertyMethods(true); + setExtractBareNamePropertyMethods(true); } @Test diff --git a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/core/DefaultDockerComposeIntegrationTests.java b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/core/DefaultDockerComposeIntegrationTests.java index 6024f5e709e..d1eeea5abf3 100644 --- a/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/core/DefaultDockerComposeIntegrationTests.java +++ b/spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/core/DefaultDockerComposeIntegrationTests.java @@ -26,7 +26,6 @@ import java.util.Collections; import java.util.List; import java.util.Set; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -38,6 +37,7 @@ import org.springframework.boot.testsupport.process.DisabledIfProcessUnavailable import org.springframework.core.io.ClassPathResource; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; /** * Tests for {@link DefaultDockerCompose}. @@ -99,13 +99,13 @@ class DefaultDockerComposeIntegrationTests { private void assertThatDoesNotContainService(List runningServices, String service) { if (findService(runningServices, service) != null) { - Assertions.fail("Did not expect service '%s', but found it in [%s]", service, runningServices); + fail("Did not expect service '%s', but found it in [%s]", service, runningServices); } } private void assertThatContainsService(List runningServices, String service) { if (findService(runningServices, service) == null) { - Assertions.fail("Expected service '%s', but hasn't been found in [%s]", service, runningServices); + fail("Expected service '%s', but hasn't been found in [%s]", service, runningServices); } } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/AbstractJsonMarshalTester.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/AbstractJsonMarshalTester.java index 505ba8890f8..67479480aa6 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/AbstractJsonMarshalTester.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/AbstractJsonMarshalTester.java @@ -26,8 +26,6 @@ import java.io.Reader; import java.io.StringReader; import java.lang.reflect.Field; -import org.assertj.core.api.Assertions; - import org.springframework.beans.factory.ObjectFactory; import org.springframework.core.ResolvableType; import org.springframework.core.io.ByteArrayResource; @@ -41,8 +39,8 @@ import org.springframework.util.ReflectionUtils; /** * Base class for AssertJ based JSON marshal testers. Exposes specific Asserts following a * {@code read}, {@code write} or {@code parse} of JSON content. Typically used in - * combination with an AssertJ {@link Assertions#assertThat(Object) assertThat} call. For - * example:
+ * combination with an AssertJ {@link org.assertj.core.api.Assertions#assertThat(Object)
+ * assertThat} call. For example: 
  * public class ExampleObjectJsonTests {
  *
  *     private AbstractJsonTester<ExampleObject> json = //...
diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java
index 2a45e85e5b6..2e51f443ff9 100644
--- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java
+++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java
@@ -18,11 +18,11 @@ package org.springframework.boot.context.properties;
 
 import java.util.function.Supplier;
 
-import org.assertj.core.api.Assertions;
 import org.junit.jupiter.api.Test;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+import static org.assertj.core.api.Assertions.fail;
 
 /**
  * Tests for {@link PropertyMapper}.
@@ -57,7 +57,7 @@ class PropertyMapperTests {
 
 	@Test
 	void fromValueAlwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
-		this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(Assertions::fail);
+		this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(this::failure);
 	}
 
 	@Test
@@ -101,14 +101,14 @@ class PropertyMapperTests {
 
 	@Test
 	void whenNonNullWhenSuppliedNullShouldNotMap() {
-		this.map.from(() -> null).whenNonNull().as(String::valueOf).toCall(Assertions::fail);
+		this.map.from(() -> null).whenNonNull().as(String::valueOf).toCall(this::failure);
 	}
 
 	@Test
 	void whenNonNullWhenSuppliedThrowsNullPointerExceptionShouldNotMap() {
 		this.map.from(() -> {
 			throw new NullPointerException();
-		}).whenNonNull().as(String::valueOf).toCall(Assertions::fail);
+		}).whenNonNull().as(String::valueOf).toCall(this::failure);
 	}
 
 	@Test
@@ -119,7 +119,7 @@ class PropertyMapperTests {
 
 	@Test
 	void whenTrueWhenValueIsFalseShouldNotMap() {
-		this.map.from(false).whenTrue().toCall(Assertions::fail);
+		this.map.from(false).whenTrue().toCall(this::failure);
 	}
 
 	@Test
@@ -130,17 +130,17 @@ class PropertyMapperTests {
 
 	@Test
 	void whenFalseWhenValueIsTrueShouldNotMap() {
-		this.map.from(true).whenFalse().toCall(Assertions::fail);
+		this.map.from(true).whenFalse().toCall(this::failure);
 	}
 
 	@Test
 	void whenHasTextWhenValueIsNullShouldNotMap() {
-		this.map.from(() -> null).whenHasText().toCall(Assertions::fail);
+		this.map.from(() -> null).whenHasText().toCall(this::failure);
 	}
 
 	@Test
 	void whenHasTextWhenValueIsEmptyShouldNotMap() {
-		this.map.from("").whenHasText().toCall(Assertions::fail);
+		this.map.from("").whenHasText().toCall(this::failure);
 	}
 
 	@Test
@@ -157,7 +157,7 @@ class PropertyMapperTests {
 
 	@Test
 	void whenEqualToWhenValueIsNotEqualShouldNotMatch() {
-		this.map.from("123").whenEqualTo("321").toCall(Assertions::fail);
+		this.map.from("123").whenEqualTo("321").toCall(this::failure);
 	}
 
 	@Test
@@ -169,7 +169,7 @@ class PropertyMapperTests {
 	@Test
 	void whenInstanceOfWhenValueIsNotTargetTypeShouldNotMatch() {
 		Supplier supplier = () -> 123L;
-		this.map.from(supplier).whenInstanceOf(Double.class).toCall(Assertions::fail);
+		this.map.from(supplier).whenInstanceOf(Double.class).toCall(this::failure);
 	}
 
 	@Test
@@ -180,7 +180,7 @@ class PropertyMapperTests {
 
 	@Test
 	void whenWhenValueDoesNotMatchShouldNotMap() {
-		this.map.from("123").when("321"::equals).toCall(Assertions::fail);
+		this.map.from("123").when("321"::equals).toCall(this::failure);
 	}
 
 	@Test
@@ -198,12 +198,12 @@ class PropertyMapperTests {
 
 	@Test
 	void alwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
-		this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(Assertions::fail);
+		this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(this::failure);
 	}
 
 	@Test
 	void whenWhenValueNotMatchesShouldSupportChainedCalls() {
-		this.map.from("123").when("456"::equals).when("123"::equals).toCall(Assertions::fail);
+		this.map.from("123").when("456"::equals).when("123"::equals).toCall(this::failure);
 	}
 
 	@Test
@@ -226,6 +226,10 @@ class PropertyMapperTests {
 		assertThat(instance).hasToString("Spring null");
 	}
 
+	private void failure() {
+		fail();
+	}
+
 	static class Count implements Supplier {
 
 		private final Supplier source;
diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/AbstractStructuredLoggingTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/AbstractStructuredLoggingTests.java
index da4c590e247..a754d920c48 100644
--- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/AbstractStructuredLoggingTests.java
+++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/AbstractStructuredLoggingTests.java
@@ -26,7 +26,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.core.impl.MutableLogEvent;
 import org.apache.logging.log4j.message.SimpleMessage;
-import org.assertj.core.api.Assertions;
 import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
 import org.mockito.junit.jupiter.MockitoExtension;
@@ -35,6 +34,7 @@ import org.springframework.boot.logging.structured.MockStructuredLoggingJsonMemb
 import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
 
 /**
  * Abstract base class for structured formatting tests.
@@ -84,7 +84,7 @@ abstract class AbstractStructuredLoggingTests {
 			});
 		}
 		catch (JsonProcessingException ex) {
-			Assertions.fail("Failed to deserialize JSON: " + json, ex);
+			fail("Failed to deserialize JSON: " + json, ex);
 			return null;
 		}
 	}
diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/AbstractStructuredLoggingTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/AbstractStructuredLoggingTests.java
index e6f97e6b7d5..867c42681a0 100644
--- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/AbstractStructuredLoggingTests.java
+++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/AbstractStructuredLoggingTests.java
@@ -29,7 +29,6 @@ import ch.qos.logback.classic.spi.ThrowableProxy;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.ObjectMapper;
-import org.assertj.core.api.Assertions;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.extension.ExtendWith;
@@ -43,6 +42,7 @@ import org.springframework.boot.logging.structured.MockStructuredLoggingJsonMemb
 import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.fail;
 
 /**
  * Abstract base class for structured formatting tests.
@@ -127,7 +127,7 @@ abstract class AbstractStructuredLoggingTests {
 			});
 		}
 		catch (JsonProcessingException ex) {
-			Assertions.fail("Failed to deserialize JSON: " + json, ex);
+			fail("Failed to deserialize JSON: " + json, ex);
 			return null;
 		}
 	}
diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml
index da4db69a466..d0129462a3c 100644
--- a/src/checkstyle/checkstyle-suppressions.xml
+++ b/src/checkstyle/checkstyle-suppressions.xml
@@ -74,4 +74,7 @@
 	
 	
 	
+	
+	
+	
 
diff --git a/src/checkstyle/checkstyle.xml b/src/checkstyle/checkstyle.xml
index 40117798128..ee8993d2393 100644
--- a/src/checkstyle/checkstyle.xml
+++ b/src/checkstyle/checkstyle.xml
@@ -77,6 +77,14 @@
 					  value="Please use Collections.emptyList()/emptyMap()/emptySet() for creating empty lists/maps/sets." />
 			
 		
+		
+			
+			
+			
+			
+			
+