mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-10-02 15:29:05 +00:00
Add checkstyle rule for AssertJ Assertions import
Add a rule to ensure we consistently use `assertThat` rather than `Assertions.assertThat`. See gh-48630
This commit is contained in:
+2
-2
@@ -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");
|
||||
}
|
||||
|
||||
|
||||
+5
-5
@@ -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<PathPattern, Object> 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
|
||||
|
||||
+3
-3
@@ -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<RunningService> 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<RunningService> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-4
@@ -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: <pre class="code">
|
||||
* combination with an AssertJ {@link org.assertj.core.api.Assertions#assertThat(Object)
|
||||
* assertThat} call. For example: <pre class="code">
|
||||
* public class ExampleObjectJsonTests {
|
||||
*
|
||||
* private AbstractJsonTester<ExampleObject> json = //...
|
||||
|
||||
+17
-13
@@ -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<Number> 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<T> implements Supplier<T> {
|
||||
|
||||
private final Supplier<T> source;
|
||||
|
||||
+2
-2
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,4 +74,7 @@
|
||||
<suppress files="ConfigurationPropertyCaching\.java" checks="SpringJavadoc" message="\@since"/>
|
||||
<suppress files="StructuredLoggingJsonMembersCustomizer\.java" checks="SpringJavadoc" message="\@since"/>
|
||||
<suppress files="FieldValues\.java" checks="SimplifyBooleanExpression" />
|
||||
<suppress files="TestPrintStream\.java" id="staticAssertJ"/>
|
||||
<suppress files="ItemMetadataAssert\.java" id="staticAssertJ"/>
|
||||
<suppress files="SpringApplicationShutdownHookInstance\.java" id="staticAssertJ"/>
|
||||
</suppressions>
|
||||
|
||||
@@ -77,6 +77,14 @@
|
||||
value="Please use Collections.emptyList()/emptyMap()/emptySet() for creating empty lists/maps/sets." />
|
||||
<property name="ignoreComments" value="true" />
|
||||
</module>
|
||||
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
|
||||
<property name="id" value="staticAssertJ"/>
|
||||
<property name="maximum" value="0"/>
|
||||
<property name="format" value="import org\.assertj\.core\.api\.Assertions;" />
|
||||
<property name="message"
|
||||
value="Please use static AssertJ imports." />
|
||||
<property name="ignoreComments" value="true" />
|
||||
</module>
|
||||
<module name="io.spring.javaformat.checkstyle.check.SpringJavadocCheck">
|
||||
<property name="publicOnlySinceTags" value="true" />
|
||||
<property name="requireSinceTag" value="true" />
|
||||
|
||||
Reference in New Issue
Block a user