Enforce use of AssertJ assumptions via Checkstyle

Closes gh-36582
This commit is contained in:
Sam Brannen
2026-04-02 11:16:42 +02:00
parent d767806977
commit b6fc3a1b6f
11 changed files with 56 additions and 44 deletions
@@ -28,7 +28,7 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.assertj.core.api.Assumptions.assumeThat;
/**
* @author Juergen Hoeller
@@ -38,7 +38,7 @@ class ServiceLoaderTests {
@BeforeAll
static void assumeDocumentBuilderFactoryCanBeLoaded() {
assumeTrue(ServiceLoader.load(DocumentBuilderFactory.class).iterator().hasNext());
assumeThat(ServiceLoader.load(DocumentBuilderFactory.class).iterator()).hasNext();
}
@Test
@@ -31,8 +31,8 @@ import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.opentest4j.TestAbortedException;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.core.testfixture.net.TestSocketUtils;
@@ -46,7 +46,7 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.assertj.core.api.Assumptions.assumeThat;
/**
* @author Rob Harrop
@@ -92,14 +92,14 @@ class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
void proxyClassIsDifferent() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy = getProxy();
assertThat(proxy.getClass()).as("The proxy class should be different than the base class").isNotSameAs(IJmxTestBean.class);
}
@Test
void differentProxiesSameClass() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy1 = getProxy();
IJmxTestBean proxy2 = getProxy();
@@ -109,7 +109,7 @@ class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
void getAttributeValue() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy1 = getProxy();
int age = proxy1.getAge();
assertThat(age).as("The age should be 100").isEqualTo(100);
@@ -117,7 +117,7 @@ class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
void setAttributeValue() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy = getProxy();
proxy.setName("Rob Harrop");
assertThat(target.getName()).as("The name of the bean should have been updated").isEqualTo("Rob Harrop");
@@ -125,35 +125,35 @@ class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
void setAttributeValueWithRuntimeException() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy = getProxy();
assertThatIllegalArgumentException().isThrownBy(() -> proxy.setName("Juergen"));
}
@Test
void setAttributeValueWithCheckedException() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy = getProxy();
assertThatExceptionOfType(ClassNotFoundException.class).isThrownBy(() -> proxy.setName("Juergen Class"));
}
@Test
void setAttributeValueWithIOException() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy = getProxy();
assertThatIOException().isThrownBy(() -> proxy.setName("Juergen IO"));
}
@Test
void setReadOnlyAttribute() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy = getProxy();
assertThatExceptionOfType(InvalidInvocationException.class).isThrownBy(() -> proxy.setAge(900));
}
@Test
void invokeNoArgs() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy = getProxy();
long result = proxy.myOperation();
assertThat(result).as("The operation should return 1").isEqualTo(1);
@@ -161,7 +161,7 @@ class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
void invokeArgs() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean proxy = getProxy();
int result = proxy.add(1, 2);
assertThat(result).as("The operation should return 3").isEqualTo(3);
@@ -169,14 +169,14 @@ class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
@Test
void invokeUnexposedMethodWithException() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
IJmxTestBean bean = getProxy();
assertThatExceptionOfType(InvalidInvocationException.class).isThrownBy(bean::dontExposeMe);
}
@Test
void lazyConnectionToRemote() throws Exception {
assumeTrue(runTests);
assumeThat(runTests).isTrue();
@SuppressWarnings("deprecation")
final int port = TestSocketUtils.findAvailableTcpPort();
@@ -199,8 +199,9 @@ class MBeanClientInterceptorTests extends AbstractMBeanServerTests {
connector.start();
}
catch (BindException ex) {
Assumptions.abort("Skipping remainder of JMX LazyConnectionToRemote test because binding to local port [" +
port + "] failed: " + ex.getMessage());
throw new TestAbortedException(
"Skipping remainder of JMX LazyConnectionToRemote test because binding to local port [%s] failed: "
.formatted(port, ex.getMessage()));
}
// should now be able to access data via the lazy proxy
@@ -27,7 +27,7 @@ import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.opentest4j.TestAbortedException;
import org.springframework.core.testfixture.net.TestSocketUtils;
@@ -58,8 +58,8 @@ class RemoteMBeanClientInterceptorTests extends MBeanClientInterceptorTests {
}
catch (BindException ex) {
runTests = false;
Assumptions.abort("Skipping remote JMX tests because binding to local port [" +
this.servicePort + "] failed: " + ex.getMessage());
throw new TestAbortedException("Skipping remote JMX tests because binding to local port [%s] failed: %s"
.formatted(this.servicePort, ex.getMessage()));
}
}
@@ -20,6 +20,8 @@ import java.util.Arrays;
import java.util.Objects;
import java.util.Set;
import org.assertj.core.api.Assumptions;
import org.assertj.core.configuration.PreferredAssumptionException;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -29,7 +31,7 @@ import static java.util.stream.Collectors.joining;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.springframework.core.testfixture.TestGroup.LONG_RUNNING;
/**
@@ -42,6 +44,10 @@ class TestGroupTests {
private static final String TEST_GROUPS_SYSTEM_PROPERTY = "testGroups";
static {
Assumptions.setPreferredAssumptionException(PreferredAssumptionException.JUNIT5);
}
private String originalTestGroups;
@@ -114,8 +120,9 @@ class TestGroupTests {
*/
private static void assumeGroup(TestGroup group) {
Set<TestGroup> testGroups = TestGroup.loadTestGroups();
assumeTrue(testGroups.contains(group),
() -> "Requires inactive test group " + group + "; active test groups: " + testGroups);
assumeThat(testGroups)
.as(() -> "Requires inactive test group " + group + "; active test groups: " + testGroups)
.contains(group);
}
}
@@ -77,9 +77,8 @@ class DefaultResponseCreatorTests {
@ParameterizedTest(name = "setBodyFromStringWithCharset [{0}]")
@ValueSource(strings = {"Cp1047", "UTF-8", "UTF-16", "US-ASCII", "ISO-8859-1"})
void setBodyFromStringWithCharset(String charset) throws IOException {
assumeThat(Charset.isSupported(charset))
.overridingErrorMessage("charset %s is not supported by this JVM", charset)
.as("charset %s is not supported by this JVM", charset)
.isTrue();
Charset charsetObj = Charset.forName(charset);
@@ -42,7 +42,6 @@ import mockwebserver3.RecordedRequest;
import okio.Buffer;
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
@@ -62,6 +61,7 @@ import org.springframework.http.ReactiveHttpOutputMessage;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.junit.jupiter.api.Named.named;
/**
@@ -203,8 +203,9 @@ class ClientHttpConnectorTests {
@ParameterizedConnectorTest
void partitionedCookieSupport(ClientHttpConnector connector) {
Assumptions.assumeFalse(connector instanceof JettyClientHttpConnector, "Jetty client does not support partitioned cookies");
Assumptions.assumeFalse(connector instanceof JdkClientHttpConnector, "JDK client does not support partitioned cookies");
assumeThat(connector).as("Jetty and JDK clients do not support partitioned cookies")
.isNotInstanceOfAny(JettyClientHttpConnector.class, JdkClientHttpConnector.class);
prepareResponse(builder -> builder
.code(200)
.addHeader("Set-Cookie", "id=test; Partitioned;"));
@@ -31,7 +31,7 @@ import org.springframework.web.testfixture.http.server.reactive.bootstrap.JettyC
import org.springframework.web.testfixture.http.server.reactive.bootstrap.ReactorHttpServer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.assertj.core.api.Assumptions.assumeThat;
/**
* @author Arjen Poutsma
@@ -51,8 +51,8 @@ class ZeroCopyIntegrationTests extends AbstractHttpHandlerIntegrationTests {
@ParameterizedHttpServerTest
void zeroCopy(HttpServer httpServer) throws Exception {
assumeTrue(httpServer instanceof ReactorHttpServer ||
httpServer instanceof JettyCoreHttpServer, "Zero-copy does not support Servlet");
assumeThat(httpServer).as("Zero-copy does not support Servlet")
.isInstanceOfAny(ReactorHttpServer.class, JettyCoreHttpServer.class);
startServer(httpServer);
@@ -65,7 +65,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.junit.jupiter.params.provider.Arguments.argumentSet;
/**
@@ -478,7 +478,9 @@ class RestClientIntegrationTests {
}
catch (HttpServerErrorException ex) {
assumeFalse(requestFactory instanceof JdkClientHttpRequestFactory, "JDK HttpClient does not expose status text");
assumeThat(requestFactory).as("JDK HttpClient does not expose status text")
.isNotInstanceOf(JdkClientHttpRequestFactory.class);
assertThat(ex.getMessage()).isEqualTo("555 Server Error: \"Something went wrong\"");
assertThat(ex.getStatusText()).isEqualTo("Server Error");
assertThat(ex.getResponseHeaders().getContentType()).isEqualTo(MediaType.TEXT_PLAIN);
@@ -60,7 +60,7 @@ import org.springframework.util.MultiValueMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.junit.jupiter.params.provider.Arguments.argumentSet;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.MediaType.MULTIPART_MIXED;
@@ -227,8 +227,8 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
@ParameterizedRestTemplateTest
void patchForObject(ClientHttpRequestFactory clientHttpRequestFactory) {
assumeFalse(clientHttpRequestFactory instanceof SimpleClientHttpRequestFactory,
"HttpURLConnection does not support the PATCH method");
assumeThat(clientHttpRequestFactory).as("HttpURLConnection does not support the PATCH method")
.isNotInstanceOf(SimpleClientHttpRequestFactory.class);
setUpClient(clientHttpRequestFactory);
@@ -265,7 +265,8 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
.satisfies(ex -> {
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(ex.getMessage()).containsSubsequence("400", "on GET request for \""+url+ "\": [no body]");
assumeFalse(clientHttpRequestFactory instanceof JdkClientHttpRequestFactory, "JDK HttpClient does not expose status text");
assumeThat(clientHttpRequestFactory).as("JDK HttpClient does not expose status text")
.isNotInstanceOf(JdkClientHttpRequestFactory.class);
assertThat(ex.getMessage()).isEqualTo("400 Client Error on GET request for \""+url+ "\": [no body]");
});
}
@@ -282,7 +283,8 @@ class RestTemplateIntegrationTests extends AbstractMockWebServerTests {
assertThat(ex.getStatusText()).isNotNull();
assertThat(ex.getResponseBodyAsString()).isNotNull();
assertThat(ex.getMessage()).containsSubsequence("500", "on GET request for \"" + url + "\": [no body]");
assumeFalse(clientHttpRequestFactory instanceof JdkClientHttpRequestFactory, "JDK HttpClient does not expose status text");
assumeThat(clientHttpRequestFactory).as("JDK HttpClient does not expose status text")
.isNotInstanceOf(JdkClientHttpRequestFactory.class);
assertThat(ex.getMessage()).isEqualTo("500 Server Error on GET request for \"" + url + "\": [no body]");
});
}
@@ -59,7 +59,7 @@ import org.springframework.web.testfixture.http.server.reactive.bootstrap.Reacto
import org.springframework.web.testfixture.http.server.reactive.bootstrap.TomcatHttpServer;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.assertj.core.api.Assumptions.assumeThat;
import static org.junit.jupiter.api.Named.named;
import static org.springframework.http.MediaType.TEXT_EVENT_STREAM;
@@ -126,7 +126,7 @@ class SseIntegrationTests extends AbstractHttpHandlerIntegrationTests {
@ParameterizedSseTest
void sseAsEvent(HttpServer httpServer, ClientHttpConnector connector) throws Exception {
assumeTrue(httpServer instanceof JettyHttpServer || httpServer instanceof JettyCoreHttpServer);
assumeThat(httpServer).isInstanceOfAny(JettyHttpServer.class, JettyCoreHttpServer.class);
startServer(httpServer, connector);
@@ -174,7 +174,7 @@ class SseIntegrationTests extends AbstractHttpHandlerIntegrationTests {
@ParameterizedSseTest // SPR-16494
@Disabled // https://github.com/reactor/reactor-netty/issues/283
void serverDetectsClientDisconnect(HttpServer httpServer, ClientHttpConnector connector) throws Exception {
assumeTrue(httpServer instanceof ReactorHttpServer);
assumeThat(httpServer).isInstanceOf(ReactorHttpServer.class);
startServer(httpServer, connector);
+2 -2
View File
@@ -261,8 +261,8 @@
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="id" value="junitJupiterAssertions"/>
<property name="maximum" value="0"/>
<property name="format" value="org\.junit\.jupiter\.api\.Assertions"/>
<property name="message" value="Please use AssertJ assertions."/>
<property name="format" value="org\.junit\.jupiter\.api\.(Assertions|Assumptions)"/>
<property name="message" value="Please use AssertJ assertions and assumptions."/>
<property name="ignoreComments" value="true"/>
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">