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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)