Skip to content

Commit 207327d

Browse files
asashourphilwebb
authored andcommitted
Use method references when possible in test code
See gh-40974
1 parent dcccb3b commit 207327d

File tree

34 files changed

+64
-48
lines changed

34 files changed

+64
-48
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/security/GraphQlWebFluxSecurityAutoConfigurationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.security.config.annotation.method.configuration.EnableReactiveMethodSecurity;
4646
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
4747
import org.springframework.security.config.web.server.ServerHttpSecurity;
48+
import org.springframework.security.config.web.server.ServerHttpSecurity.CsrfSpec;
4849
import org.springframework.security.core.userdetails.MapReactiveUserDetailsService;
4950
import org.springframework.security.core.userdetails.User;
5051
import org.springframework.security.core.userdetails.UserDetails;
@@ -161,7 +162,7 @@ static class SecurityConfig {
161162

162163
@Bean
163164
SecurityWebFilterChain springWebFilterChain(ServerHttpSecurity http) {
164-
return http.csrf((spec) -> spec.disable())
165+
return http.csrf(CsrfSpec::disable)
165166
// Demonstrate that method security works
166167
// Best practice to use both for defense in depth
167168
.authorizeExchange((requests) -> requests.anyExchange().permitAll())

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/security/GraphQlWebMvcSecurityAutoConfigurationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
4242
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
4343
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
44+
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
4445
import org.springframework.security.core.userdetails.User;
4546
import org.springframework.security.core.userdetails.UserDetails;
4647
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@@ -154,7 +155,7 @@ static class SecurityConfig {
154155

155156
@Bean
156157
DefaultSecurityFilterChain springWebFilterChain(HttpSecurity http) throws Exception {
157-
return http.csrf((c) -> c.disable())
158+
return http.csrf(CsrfConfigurer::disable)
158159
// Demonstrate that method security works
159160
// Best practice to use both for defense in depth
160161
.authorizeHttpRequests((requests) -> requests.anyRequest().permitAll())

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ void logsDebugOnContextRefresh(CapturedOutput output) {
5858
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
5959
this.initializer.initialize(context);
6060
context.register(Config.class);
61-
withDebugLogging(() -> context.refresh());
61+
withDebugLogging(context::refresh);
6262
assertThat(output).contains("CONDITIONS EVALUATION REPORT");
6363
}
6464

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/filewatch/FileSystemWatcherTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ private void setupWatcher(long pollingInterval, long quietPeriod) {
297297
private void setupWatcher(long pollingInterval, long quietPeriod, SnapshotStateRepository snapshotStateRepository) {
298298
this.watcher = new FileSystemWatcher(false, Duration.ofMillis(pollingInterval), Duration.ofMillis(quietPeriod),
299299
snapshotStateRepository);
300-
this.watcher.addListener((changeSet) -> FileSystemWatcherTests.this.changes.add(changeSet));
300+
this.watcher.addListener(FileSystemWatcherTests.this.changes::add);
301301
}
302302

303303
private File startWithNewDirectory() {

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/core/DefaultRunningServiceTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ void toStringReturnsServiceName() {
115115
}
116116

117117
private DefaultRunningService createRunningService(boolean psResponseHasImage) {
118-
DockerHost host = DockerHost.get("192.168.1.1", () -> Collections.emptyList());
118+
DockerHost host = DockerHost.get("192.168.1.1", Collections::emptyList);
119119
String id = "123";
120120
String name = "my-service";
121121
String image = "redis";

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/core/DockerHostTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class DockerHostTests {
5252

5353
private static final Function<String, String> NO_SYSTEM_ENV = (key) -> null;
5454

55-
private static final Supplier<List<DockerCliContextResponse>> NO_CONTEXT = () -> Collections.emptyList();
55+
private static final Supplier<List<DockerCliContextResponse>> NO_CONTEXT = Collections::emptyList;
5656

5757
@Test
5858
void getWhenHasHost() {

spring-boot-project/spring-boot-docker-compose/src/test/java/org/springframework/boot/docker/compose/lifecycle/TcpConnectServiceReadinessCheckTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ void setup() {
5858

5959
@Test
6060
void checkWhenServerWritesData() throws Exception {
61-
withServer((socket) -> socket.getOutputStream().write('!'), (port) -> check(port));
61+
withServer((socket) -> socket.getOutputStream().write('!'), this::check);
6262
}
6363

6464
@Test
6565
void checkWhenNoSocketOutput() throws Exception {
6666
// Simulate waiting for traffic from client to server. The sleep duration must
6767
// be longer than the read timeout of the ready check!
68-
withServer((socket) -> sleep(Duration.ofSeconds(10)), (port) -> check(port));
68+
withServer((socket) -> sleep(Duration.ofSeconds(10)), this::check);
6969
}
7070

7171
@Test

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/context/filter/ExcludeFilterApplicationContextInitializerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ExcludeFilterApplicationContextInitializerTests {
4040
void testConfigurationIsExcluded() {
4141
SpringApplication application = new SpringApplication(TestApplication.class);
4242
application.setWebApplicationType(WebApplicationType.NONE);
43-
AssertableApplicationContext applicationContext = AssertableApplicationContext.get(() -> application.run());
43+
AssertableApplicationContext applicationContext = AssertableApplicationContext.get(application::run);
4444
assertThat(applicationContext).hasSingleBean(TestApplication.class);
4545
assertThat(applicationContext).doesNotHaveBean(ExcludedTestConfiguration.class);
4646
}

spring-boot-project/spring-boot-testcontainers/src/test/java/org/springframework/boot/testcontainers/service/connection/ContainerConnectionDetailsFactoryTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void getConnectionDetailsHasOrigin() {
111111
void getContainerWhenNotInitializedThrowsException() {
112112
TestContainerConnectionDetailsFactory factory = new TestContainerConnectionDetailsFactory();
113113
TestContainerConnectionDetails connectionDetails = getConnectionDetails(factory, this.source);
114-
assertThatIllegalStateException().isThrownBy(() -> connectionDetails.callGetContainer())
114+
assertThatIllegalStateException().isThrownBy(connectionDetails::callGetContainer)
115115
.withMessage("Container cannot be obtained before the connection details bean has been initialized");
116116
}
117117

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/type/ImageReferenceTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ void randomWherePrefixIsNullThrowsException() {
255255
void inTaggedFormWhenHasDigestThrowsException() {
256256
ImageReference reference = ImageReference
257257
.of("ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d");
258-
assertThatIllegalStateException().isThrownBy(() -> reference.inTaggedForm())
258+
assertThatIllegalStateException().isThrownBy(reference::inTaggedForm)
259259
.withMessage(
260260
"Image reference 'docker.io/library/ubuntu@sha256:6e9f67fa63b0323e9a1e587fd71c561ba48a034504fb804fd26fd8800039835d' cannot contain a digest");
261261
}

spring-boot-project/spring-boot-tools/spring-boot-loader-classic/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ void iteratorWhenClosedLater() throws IOException {
719719
Iterator<JarEntry> iterator = this.jarFile.iterator();
720720
iterator.next();
721721
this.jarFile.close();
722-
assertThatZipFileClosedIsThrownBy(() -> iterator.hasNext());
722+
assertThatZipFileClosedIsThrownBy(iterator::hasNext);
723723
}
724724

725725
@Test

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/NestedJarFileTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ void getCommentReturnsComment() throws IOException {
240240
void getCommentWhenClosedThrowsException() throws IOException {
241241
try (NestedJarFile jar = new NestedJarFile(this.file)) {
242242
jar.close();
243-
assertThatIllegalStateException().isThrownBy(() -> jar.getComment()).withMessage("Zip file closed");
243+
assertThatIllegalStateException().isThrownBy(jar::getComment).withMessage("Zip file closed");
244244
}
245245
}
246246

@@ -269,7 +269,7 @@ void sizeReturnsSize() throws IOException {
269269
void sizeWhenClosedThrowsException() throws Exception {
270270
try (NestedJarFile jar = new NestedJarFile(this.file)) {
271271
jar.close();
272-
assertThatIllegalStateException().isThrownBy(() -> jar.size()).withMessage("Zip file closed");
272+
assertThatIllegalStateException().isThrownBy(jar::size).withMessage("Zip file closed");
273273
}
274274
}
275275

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/net/protocol/jar/JarUrlConnectionTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void getPermissionReturnJarConnectionPermission() throws IOException {
242242
@Test
243243
void getInputStreamWhenNotNestedAndHasNoEntryThrowsException() throws Exception {
244244
JarUrlConnection connection = JarUrlConnection.open(JarUrl.create(this.file));
245-
assertThatIOException().isThrownBy(() -> connection.getInputStream()).withMessage("no entry name specified");
245+
assertThatIOException().isThrownBy(connection::getInputStream).withMessage("no entry name specified");
246246
}
247247

248248
@Test

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/test/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathExtensionForkParameterizedTests.java

+8-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,14 @@ class ModifiedClassPathExtensionForkParameterizedTests {
3737
@ParameterizedTest
3838
@ValueSource(strings = { "one", "two", "three" })
3939
void testIsInvokedOnceForEachArgument(String argument) {
40-
switch (argument) {
41-
case "one" -> assertThat(arguments).isEmpty();
42-
case "two" -> assertThat(arguments).doesNotContain("two", "three");
43-
case "three" -> assertThat(arguments).doesNotContain("three");
40+
if (argument.equals("one")) {
41+
assertThat(arguments).isEmpty();
42+
}
43+
else if (argument.equals("two")) {
44+
assertThat(arguments).doesNotContain("two", "three");
45+
}
46+
else if (argument.equals("three")) {
47+
assertThat(arguments).doesNotContain("three");
4448
}
4549
arguments.add(argument);
4650
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void getPropertySourcesWhenUnavailableThrowsException() {
7979
Environment environment = mock(Environment.class);
8080
given(applicationContext.getEnvironment()).willReturn(environment);
8181
PropertySourcesDeducer deducer = new PropertySourcesDeducer(applicationContext);
82-
assertThatIllegalStateException().isThrownBy(() -> deducer.getPropertySources())
82+
assertThatIllegalStateException().isThrownBy(deducer::getPropertySources)
8383
.withMessage("Unable to obtain PropertySources from PropertySourcesPlaceholderConfigurer or Environment");
8484
}
8585

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ void getNameWhenCustomReturnsName() {
160160
@Test
161161
void getLevelWhenCustomThrowsException() {
162162
LevelConfiguration configuration = LevelConfiguration.ofCustom("FINE");
163-
assertThatIllegalStateException().isThrownBy(() -> configuration.getLevel())
163+
assertThatIllegalStateException().isThrownBy(configuration::getLevel)
164164
.withMessage("Unable to provide LogLevel for 'FINE'");
165165
}
166166

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.logging.log4j2;
1818

19+
import java.lang.reflect.Method;
1920
import java.util.stream.Stream;
2021

2122
import org.apache.logging.log4j.LogManager;
@@ -46,8 +47,7 @@ void propertySourceHasDisabledShutdownHook() {
4647

4748
@Test
4849
void allDefaultMethodsAreImplemented() {
49-
assertThat(Stream.of(SpringBootPropertySource.class.getMethods()).filter((method) -> method.isDefault()))
50-
.isEmpty();
50+
assertThat(Stream.of(SpringBootPropertySource.class.getMethods()).filter(Method::isDefault)).isEmpty();
5151
}
5252

5353
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void noExceptionsAreThrownWhenBeforeInitializeIsCalledInParallel() {
5353
List<Thread> threads = new ArrayList<>();
5454
List<Throwable> exceptions = new CopyOnWriteArrayList<>();
5555
for (int i = 0; i < 10; i++) {
56-
Thread thread = new Thread(() -> this.loggingSystem.beforeInitialize());
56+
Thread thread = new Thread(this.loggingSystem::beforeInitialize);
5757
thread.setUncaughtExceptionHandler((t, ex) -> exceptions.add(ex));
5858
threads.add(thread);
5959
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/sql/init/AbstractScriptDatabaseInitializerTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void whenContinueOnErrorIsFalseThenInitializationFailsOnError() {
5959
DatabaseInitializationSettings settings = new DatabaseInitializationSettings();
6060
settings.setDataLocations(Arrays.asList("data.sql"));
6161
T initializer = createEmbeddedDatabaseInitializer(settings);
62-
assertThatExceptionOfType(DataAccessException.class).isThrownBy(() -> initializer.initializeDatabase());
62+
assertThatExceptionOfType(DataAccessException.class).isThrownBy(initializer::initializeDatabase);
6363
assertThatDatabaseWasAccessed(initializer);
6464
}
6565

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ssl/DefaultSslManagerBundleTests.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void getKeyManagerFactoryWhenHasAliasNotInStoreThrowsException() throws Exceptio
8989
SslStoreBundle storeBundle = SslStoreBundle.of(keyStore, null, null);
9090
DefaultSslManagerBundle bundle = new TestDefaultSslManagerBundle(storeBundle,
9191
SslBundleKey.of("secret", "alias"));
92-
assertThatIllegalStateException().isThrownBy(() -> bundle.getKeyManagerFactory())
92+
assertThatIllegalStateException().isThrownBy(bundle::getKeyManagerFactory)
9393
.withMessage("Keystore does not contain alias 'alias'");
9494
}
9595

@@ -100,7 +100,7 @@ void getKeyManagerFactoryWhenHasAliasNotDeterminedInStoreThrowsException() throw
100100
SslStoreBundle storeBundle = SslStoreBundle.of(keyStore, null, null);
101101
DefaultSslManagerBundle bundle = new TestDefaultSslManagerBundle(storeBundle,
102102
SslBundleKey.of("secret", "alias"));
103-
assertThatIllegalStateException().isThrownBy(() -> bundle.getKeyManagerFactory())
103+
assertThatIllegalStateException().isThrownBy(bundle::getKeyManagerFactory)
104104
.withMessage("Could not determine if keystore contains alias 'alias'");
105105
}
106106

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/reactive/context/ReactiveWebServerApplicationContextTests.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void cleanUp() {
5959

6060
@Test
6161
void whenThereIsNoWebServerFactoryBeanThenContextRefreshWillFail() {
62-
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
62+
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
6363
.havingRootCause()
6464
.withMessageContaining(
6565
"Unable to start ReactiveWebServerApplicationContext due to missing ReactiveWebServerFactory bean");
@@ -68,7 +68,7 @@ void whenThereIsNoWebServerFactoryBeanThenContextRefreshWillFail() {
6868
@Test
6969
void whenThereIsNoHttpHandlerBeanThenContextRefreshWillFail() {
7070
addWebServerFactoryBean();
71-
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
71+
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
7272
.havingRootCause()
7373
.withMessageContaining("Unable to start ReactiveWebApplicationContext due to missing HttpHandler bean");
7474
}
@@ -77,7 +77,7 @@ void whenThereIsNoHttpHandlerBeanThenContextRefreshWillFail() {
7777
void whenThereAreMultipleWebServerFactoryBeansThenContextRefreshWillFail() {
7878
addWebServerFactoryBean();
7979
addWebServerFactoryBean("anotherWebServerFactory");
80-
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
80+
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
8181
.havingRootCause()
8282
.withMessageContaining(
8383
"Unable to start ReactiveWebApplicationContext due to multiple ReactiveWebServerFactory beans");
@@ -88,7 +88,7 @@ void whenThereAreMultipleHttpHandlerBeansThenContextRefreshWillFail() {
8888
addWebServerFactoryBean();
8989
addHttpHandlerBean("httpHandler1");
9090
addHttpHandlerBean("httpHandler2");
91-
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
91+
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
9292
.havingRootCause()
9393
.withMessageContaining("Unable to start ReactiveWebApplicationContext due to multiple HttpHandler beans");
9494
}
@@ -164,7 +164,7 @@ void whenTheContextIsRefreshedThenASubsequentRefreshAttemptWillFail() {
164164
addWebServerFactoryBean();
165165
addHttpHandlerBean();
166166
this.context.refresh();
167-
assertThatIllegalStateException().isThrownBy(() -> this.context.refresh())
167+
assertThatIllegalStateException().isThrownBy(this.context::refresh)
168168
.withMessageContaining("multiple refresh attempts");
169169
}
170170

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/AbstractFilterRegistrationBeanTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void withSpecificDispatcherTypesEnumSet() throws Exception {
205205

206206
@Test
207207
void failsWithDoubleRegistration() {
208-
assertThatIllegalStateException().isThrownBy(() -> doubleRegistration())
208+
assertThatIllegalStateException().isThrownBy(this::doubleRegistration)
209209
.withMessage("Failed to register 'filter double-registration' on the "
210210
+ "servlet context. Possibly already registered?");
211211
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletRegistrationBeanTests.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void startupWithDefaults() throws Exception {
6969

7070
@Test
7171
void failsWithDoubleRegistration() {
72-
assertThatIllegalStateException().isThrownBy(() -> doubleRegistration())
72+
assertThatIllegalStateException().isThrownBy(this::doubleRegistration)
7373
.withMessage("Failed to register 'servlet double-registration' on "
7474
+ "the servlet context. Possibly already registered?");
7575
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/context/ServletWebServerApplicationContextTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ void whenContextIsNotActiveThenCloseDoesNotChangeTheApplicationAvailability() {
215215
void cannotSecondRefresh() {
216216
addWebServerFactoryBean();
217217
this.context.refresh();
218-
assertThatIllegalStateException().isThrownBy(() -> this.context.refresh());
218+
assertThatIllegalStateException().isThrownBy(this.context::refresh);
219219
}
220220

221221
@Test
@@ -229,7 +229,7 @@ void servletContextAwareBeansAreInjected() {
229229

230230
@Test
231231
void missingServletWebServerFactory() {
232-
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
232+
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
233233
.havingRootCause()
234234
.withMessageContaining("Unable to start ServletWebServerApplicationContext due to missing "
235235
+ "ServletWebServerFactory bean");
@@ -240,7 +240,7 @@ void tooManyWebServerFactories() {
240240
addWebServerFactoryBean();
241241
this.context.registerBeanDefinition("webServerFactory2",
242242
new RootBeanDefinition(MockServletWebServerFactory.class));
243-
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(() -> this.context.refresh())
243+
assertThatExceptionOfType(ApplicationContextException.class).isThrownBy(this.context::refresh)
244244
.havingRootCause()
245245
.withMessageContaining("Unable to start ServletWebServerApplicationContext due to "
246246
+ "multiple ServletWebServerFactory beans");

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-actuator/src/test/java/smoketest/actuator/ShutdownSampleActuatorApplicationTests.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.http.HttpStatus;
3030
import org.springframework.http.ResponseEntity;
3131
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
32+
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
3233
import org.springframework.security.web.SecurityFilterChain;
3334
import org.springframework.test.annotation.DirtiesContext;
3435

@@ -74,7 +75,7 @@ static class SecurityConfiguration {
7475

7576
@Bean
7677
SecurityFilterChain configure(HttpSecurity http) throws Exception {
77-
http.csrf((csrf) -> csrf.disable());
78+
http.csrf(CsrfConfigurer::disable);
7879
return http.build();
7980
}
8081

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-graphql/src/main/java/smoketest/graphql/SecurityConfig.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity;
2222
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
2323
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
24+
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
2425
import org.springframework.security.core.userdetails.User;
2526
import org.springframework.security.core.userdetails.UserDetails;
2627
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@@ -35,7 +36,7 @@ public class SecurityConfig {
3536

3637
@Bean
3738
public DefaultSecurityFilterChain springWebFilterChain(HttpSecurity http) throws Exception {
38-
return http.csrf((csrf) -> csrf.disable())
39+
return http.csrf(CsrfConfigurer::disable)
3940
// Demonstrate that method security works
4041
// Best practice to use both for defense in depth
4142
.authorizeHttpRequests((requests) -> requests.anyRequest().permitAll())

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-hazelcast/src/main/java/smoketest/session/hazelcast/SecurityConfiguration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.context.annotation.Bean;
2222
import org.springframework.context.annotation.Configuration;
2323
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
24+
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
2425
import org.springframework.security.web.SecurityFilterChain;
2526

2627
import static org.springframework.security.config.Customizer.withDefaults;
@@ -41,7 +42,7 @@ SecurityFilterChain managementSecurityFilterChain(HttpSecurity http) throws Exce
4142
});
4243
http.formLogin(withDefaults());
4344
http.httpBasic(withDefaults());
44-
http.csrf((csrf) -> csrf.disable());
45+
http.csrf(CsrfConfigurer::disable);
4546
return http.build();
4647
}
4748

spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-session-jdbc/src/main/java/smoketest/session/SecurityConfiguration.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.context.annotation.Bean;
2222
import org.springframework.context.annotation.Configuration;
2323
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
24+
import org.springframework.security.config.annotation.web.configurers.CsrfConfigurer;
2425
import org.springframework.security.web.SecurityFilterChain;
2526

2627
import static org.springframework.security.config.Customizer.withDefaults;
@@ -41,7 +42,7 @@ SecurityFilterChain managementSecurityFilterChain(HttpSecurity http) throws Exce
4142
});
4243
http.formLogin(withDefaults());
4344
http.httpBasic(withDefaults());
44-
http.csrf((csrf) -> csrf.disable());
45+
http.csrf(CsrfConfigurer::disable);
4546
return http.build();
4647
}
4748

0 commit comments

Comments
 (0)