Skip to content

Commit 92263e7

Browse files
committed
Add checkstyle rule for AssertJ Assertions import
Add a rule to ensure we consistently use `assertThat` rather than `Assertions.assertThat`. See gh-48630
1 parent c10cf59 commit 92263e7

9 files changed

Lines changed: 44 additions & 31 deletions

File tree

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/BraveAutoConfigurationTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import io.micrometer.tracing.exporter.SpanExportingPredicate;
4747
import io.micrometer.tracing.exporter.SpanFilter;
4848
import io.micrometer.tracing.exporter.SpanReporter;
49-
import org.assertj.core.api.Assertions;
5049
import org.assertj.core.api.InstanceOfAssertFactories;
5150
import org.junit.jupiter.api.Test;
5251

@@ -62,6 +61,7 @@
6261

6362
import static org.assertj.core.api.Assertions.assertThat;
6463
import static org.assertj.core.api.Assertions.assertThatException;
64+
import static org.assertj.core.api.Assertions.fail;
6565
import static org.mockito.Mockito.mock;
6666

6767
/**
@@ -400,7 +400,7 @@ private List<Factory> getInjectors(Factory factory) {
400400
if (factory instanceof CompositePropagationFactory compositePropagationFactory) {
401401
return compositePropagationFactory.getInjectors().toList();
402402
}
403-
Assertions.fail("Expected CompositePropagationFactory, found %s".formatted(factory.getClass()));
403+
fail("Expected CompositePropagationFactory, found %s".formatted(factory.getClass()));
404404
throw new AssertionError("Unreachable");
405405
}
406406

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import org.aspectj.lang.JoinPoint;
3737
import org.aspectj.lang.annotation.AfterReturning;
3838
import org.aspectj.lang.annotation.Aspect;
39-
import org.assertj.core.api.Assertions;
4039
import org.assertj.core.api.InstanceOfAssertFactories;
4140
import org.junit.jupiter.api.Test;
4241
import org.junit.jupiter.api.condition.EnabledForJreRange;
@@ -122,6 +121,7 @@
122121
import org.springframework.web.util.pattern.PathPattern;
123122

124123
import static org.assertj.core.api.Assertions.assertThat;
124+
import static org.assertj.core.api.Assertions.setExtractBareNamePropertyMethods;
125125
import static org.mockito.ArgumentMatchers.any;
126126
import static org.mockito.BDDMockito.then;
127127
import static org.mockito.Mockito.mock;
@@ -475,7 +475,7 @@ void multipleWebFluxRegistrations() {
475475

476476
@Test
477477
void cachePeriod() {
478-
Assertions.setExtractBareNamePropertyMethods(false);
478+
setExtractBareNamePropertyMethods(false);
479479
this.contextRunner.withPropertyValues("spring.web.resources.cache.period:5").run((context) -> {
480480
Map<PathPattern, Object> handlerMap = getHandlerMap(context);
481481
assertThat(handlerMap).hasSize(2);
@@ -486,12 +486,12 @@ void cachePeriod() {
486486
}
487487
}
488488
});
489-
Assertions.setExtractBareNamePropertyMethods(true);
489+
setExtractBareNamePropertyMethods(true);
490490
}
491491

492492
@Test
493493
void cacheControl() {
494-
Assertions.setExtractBareNamePropertyMethods(false);
494+
setExtractBareNamePropertyMethods(false);
495495
this.contextRunner
496496
.withPropertyValues("spring.web.resources.cache.cachecontrol.max-age:5",
497497
"spring.web.resources.cache.cachecontrol.proxy-revalidate:true")
@@ -505,7 +505,7 @@ void cacheControl() {
505505
}
506506
}
507507
});
508-
Assertions.setExtractBareNamePropertyMethods(true);
508+
setExtractBareNamePropertyMethods(true);
509509
}
510510

511511
@Test

spring-boot-project/spring-boot-docker-compose/src/dockerTest/java/org/springframework/boot/docker/compose/core/DefaultDockerComposeIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.List;
2727
import java.util.Set;
2828

29-
import org.assertj.core.api.Assertions;
3029
import org.junit.jupiter.api.Test;
3130
import org.junit.jupiter.api.io.TempDir;
3231

@@ -38,6 +37,7 @@
3837
import org.springframework.core.io.ClassPathResource;
3938

4039
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.assertj.core.api.Assertions.fail;
4141

4242
/**
4343
* Tests for {@link DefaultDockerCompose}.
@@ -99,13 +99,13 @@ private RunningService findService(List<RunningService> runningServices, String
9999

100100
private void assertThatDoesNotContainService(List<RunningService> runningServices, String service) {
101101
if (findService(runningServices, service) != null) {
102-
Assertions.fail("Did not expect service '%s', but found it in [%s]", service, runningServices);
102+
fail("Did not expect service '%s', but found it in [%s]", service, runningServices);
103103
}
104104
}
105105

106106
private void assertThatContainsService(List<RunningService> runningServices, String service) {
107107
if (findService(runningServices, service) == null) {
108-
Assertions.fail("Expected service '%s', but hasn't been found in [%s]", service, runningServices);
108+
fail("Expected service '%s', but hasn't been found in [%s]", service, runningServices);
109109
}
110110
}
111111

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/json/AbstractJsonMarshalTester.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import java.io.StringReader;
2727
import java.lang.reflect.Field;
2828

29-
import org.assertj.core.api.Assertions;
30-
3129
import org.springframework.beans.factory.ObjectFactory;
3230
import org.springframework.core.ResolvableType;
3331
import org.springframework.core.io.ByteArrayResource;
@@ -41,8 +39,8 @@
4139
/**
4240
* Base class for AssertJ based JSON marshal testers. Exposes specific Asserts following a
4341
* {@code read}, {@code write} or {@code parse} of JSON content. Typically used in
44-
* combination with an AssertJ {@link Assertions#assertThat(Object) assertThat} call. For
45-
* example: <pre class="code">
42+
* combination with an AssertJ {@link org.assertj.core.api.Assertions#assertThat(Object)
43+
* assertThat} call. For example: <pre class="code">
4644
* public class ExampleObjectJsonTests {
4745
*
4846
* private AbstractJsonTester&lt;ExampleObject&gt; json = //...

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/PropertyMapperTests.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import java.util.function.Supplier;
2020

21-
import org.assertj.core.api.Assertions;
2221
import org.junit.jupiter.api.Test;
2322

2423
import static org.assertj.core.api.Assertions.assertThat;
2524
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
25+
import static org.assertj.core.api.Assertions.fail;
2626

2727
/**
2828
* Tests for {@link PropertyMapper}.
@@ -57,7 +57,7 @@ void fromValueAsIntShouldAdaptValue() {
5757

5858
@Test
5959
void fromValueAlwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
60-
this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(Assertions::fail);
60+
this.map.alwaysApplyingWhenNonNull().from((String) null).toCall(this::failure);
6161
}
6262

6363
@Test
@@ -101,14 +101,14 @@ void asShouldAdaptSupplier() {
101101

102102
@Test
103103
void whenNonNullWhenSuppliedNullShouldNotMap() {
104-
this.map.from(() -> null).whenNonNull().as(String::valueOf).toCall(Assertions::fail);
104+
this.map.from(() -> null).whenNonNull().as(String::valueOf).toCall(this::failure);
105105
}
106106

107107
@Test
108108
void whenNonNullWhenSuppliedThrowsNullPointerExceptionShouldNotMap() {
109109
this.map.from(() -> {
110110
throw new NullPointerException();
111-
}).whenNonNull().as(String::valueOf).toCall(Assertions::fail);
111+
}).whenNonNull().as(String::valueOf).toCall(this::failure);
112112
}
113113

114114
@Test
@@ -119,7 +119,7 @@ void whenTrueWhenValueIsTrueShouldMap() {
119119

120120
@Test
121121
void whenTrueWhenValueIsFalseShouldNotMap() {
122-
this.map.from(false).whenTrue().toCall(Assertions::fail);
122+
this.map.from(false).whenTrue().toCall(this::failure);
123123
}
124124

125125
@Test
@@ -130,17 +130,17 @@ void whenFalseWhenValueIsFalseShouldMap() {
130130

131131
@Test
132132
void whenFalseWhenValueIsTrueShouldNotMap() {
133-
this.map.from(true).whenFalse().toCall(Assertions::fail);
133+
this.map.from(true).whenFalse().toCall(this::failure);
134134
}
135135

136136
@Test
137137
void whenHasTextWhenValueIsNullShouldNotMap() {
138-
this.map.from(() -> null).whenHasText().toCall(Assertions::fail);
138+
this.map.from(() -> null).whenHasText().toCall(this::failure);
139139
}
140140

141141
@Test
142142
void whenHasTextWhenValueIsEmptyShouldNotMap() {
143-
this.map.from("").whenHasText().toCall(Assertions::fail);
143+
this.map.from("").whenHasText().toCall(this::failure);
144144
}
145145

146146
@Test
@@ -157,7 +157,7 @@ void whenEqualToWhenValueIsEqualShouldMatch() {
157157

158158
@Test
159159
void whenEqualToWhenValueIsNotEqualShouldNotMatch() {
160-
this.map.from("123").whenEqualTo("321").toCall(Assertions::fail);
160+
this.map.from("123").whenEqualTo("321").toCall(this::failure);
161161
}
162162

163163
@Test
@@ -169,7 +169,7 @@ void whenInstanceOfWhenValueIsTargetTypeShouldMatch() {
169169
@Test
170170
void whenInstanceOfWhenValueIsNotTargetTypeShouldNotMatch() {
171171
Supplier<Number> supplier = () -> 123L;
172-
this.map.from(supplier).whenInstanceOf(Double.class).toCall(Assertions::fail);
172+
this.map.from(supplier).whenInstanceOf(Double.class).toCall(this::failure);
173173
}
174174

175175
@Test
@@ -180,7 +180,7 @@ void whenWhenValueMatchesShouldMap() {
180180

181181
@Test
182182
void whenWhenValueDoesNotMatchShouldNotMap() {
183-
this.map.from("123").when("321"::equals).toCall(Assertions::fail);
183+
this.map.from("123").when("321"::equals).toCall(this::failure);
184184
}
185185

186186
@Test
@@ -198,12 +198,12 @@ void whenWhenCombinedWithAsUsesSourceValue() {
198198

199199
@Test
200200
void alwaysApplyingWhenNonNullShouldAlwaysApplyNonNullToSource() {
201-
this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(Assertions::fail);
201+
this.map.alwaysApplyingWhenNonNull().from(() -> null).toCall(this::failure);
202202
}
203203

204204
@Test
205205
void whenWhenValueNotMatchesShouldSupportChainedCalls() {
206-
this.map.from("123").when("456"::equals).when("123"::equals).toCall(Assertions::fail);
206+
this.map.from("123").when("456"::equals).when("123"::equals).toCall(this::failure);
207207
}
208208

209209
@Test
@@ -226,6 +226,10 @@ void toImmutableWhenFilteredReturnsOriginalInstance() {
226226
assertThat(instance).hasToString("Spring null");
227227
}
228228

229+
private void failure() {
230+
fail();
231+
}
232+
229233
static class Count<T> implements Supplier<T> {
230234

231235
private final Supplier<T> source;

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/AbstractStructuredLoggingTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.apache.logging.log4j.Level;
2727
import org.apache.logging.log4j.core.impl.MutableLogEvent;
2828
import org.apache.logging.log4j.message.SimpleMessage;
29-
import org.assertj.core.api.Assertions;
3029
import org.junit.jupiter.api.extension.ExtendWith;
3130
import org.mockito.Mock;
3231
import org.mockito.junit.jupiter.MockitoExtension;
@@ -35,6 +34,7 @@
3534
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
3635

3736
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.assertj.core.api.Assertions.fail;
3838

3939
/**
4040
* Abstract base class for structured formatting tests.
@@ -84,7 +84,7 @@ protected Map<String, Object> deserialize(String json) {
8484
});
8585
}
8686
catch (JsonProcessingException ex) {
87-
Assertions.fail("Failed to deserialize JSON: " + json, ex);
87+
fail("Failed to deserialize JSON: " + json, ex);
8888
return null;
8989
}
9090
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/AbstractStructuredLoggingTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import com.fasterxml.jackson.core.JsonProcessingException;
3030
import com.fasterxml.jackson.core.type.TypeReference;
3131
import com.fasterxml.jackson.databind.ObjectMapper;
32-
import org.assertj.core.api.Assertions;
3332
import org.junit.jupiter.api.AfterEach;
3433
import org.junit.jupiter.api.BeforeEach;
3534
import org.junit.jupiter.api.extension.ExtendWith;
@@ -43,6 +42,7 @@
4342
import org.springframework.boot.logging.structured.StructuredLoggingJsonMembersCustomizer;
4443

4544
import static org.assertj.core.api.Assertions.assertThat;
45+
import static org.assertj.core.api.Assertions.fail;
4646

4747
/**
4848
* Abstract base class for structured formatting tests.
@@ -127,7 +127,7 @@ protected Map<String, Object> deserialize(String json) {
127127
});
128128
}
129129
catch (JsonProcessingException ex) {
130-
Assertions.fail("Failed to deserialize JSON: " + json, ex);
130+
fail("Failed to deserialize JSON: " + json, ex);
131131
return null;
132132
}
133133
}

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@
7474
<suppress files="ConfigurationPropertyCaching\.java" checks="SpringJavadoc" message="\@since"/>
7575
<suppress files="StructuredLoggingJsonMembersCustomizer\.java" checks="SpringJavadoc" message="\@since"/>
7676
<suppress files="FieldValues\.java" checks="SimplifyBooleanExpression" />
77+
<suppress files="TestPrintStream\.java" id="staticAssertJ"/>
78+
<suppress files="ItemMetadataAssert\.java" id="staticAssertJ"/>
79+
<suppress files="SpringApplicationShutdownHookInstance\.java" id="staticAssertJ"/>
7780
</suppressions>

src/checkstyle/checkstyle.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@
7777
value="Please use Collections.emptyList()/emptyMap()/emptySet() for creating empty lists/maps/sets." />
7878
<property name="ignoreComments" value="true" />
7979
</module>
80+
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
81+
<property name="id" value="staticAssertJ"/>
82+
<property name="maximum" value="0"/>
83+
<property name="format" value="import org\.assertj\.core\.api\.Assertions;" />
84+
<property name="message"
85+
value="Please use static AssertJ imports." />
86+
<property name="ignoreComments" value="true" />
87+
</module>
8088
<module name="io.spring.javaformat.checkstyle.check.SpringJavadocCheck">
8189
<property name="publicOnlySinceTags" value="true" />
8290
<property name="requireSinceTag" value="true" />

0 commit comments

Comments
 (0)