Skip to content

Commit 23ab9ca

Browse files
kth496sbrannen
authored andcommitted
Refactor tests to use lambda expressions & method references
Closes gh-27386
1 parent 51d263b commit 23ab9ca

File tree

9 files changed

+11
-34
lines changed

9 files changed

+11
-34
lines changed

spring-core/src/test/java/org/springframework/core/annotation/MissingMergedAnnotationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void getDefaultValueReturnsEmpty() {
257257

258258
@Test
259259
void synthesizeThrowsNoSuchElementException() {
260-
assertThatNoSuchElementException().isThrownBy(() -> this.missing.synthesize());
260+
assertThatNoSuchElementException().isThrownBy(this.missing::synthesize);
261261
}
262262

263263
@Test

spring-core/src/test/java/org/springframework/core/convert/support/GenericConversionServiceTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,7 @@ void convertWrongTypeArgument() {
190190

191191
@Test
192192
void convertSuperSourceType() {
193-
conversionService.addConverter(new Converter<CharSequence, Integer>() {
194-
@Override
195-
public Integer convert(CharSequence source) {
196-
return Integer.valueOf(source.toString());
197-
}
198-
});
193+
conversionService.addConverter(CharSequence.class, Integer.class, source -> Integer.valueOf(source.toString()));
199194
Integer result = conversionService.convert("3", Integer.class);
200195
assertThat((int) result).isEqualTo((int) Integer.valueOf(3));
201196
}

spring-core/src/test/java/org/springframework/core/convert/support/StreamConverterTests.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.core.convert.ConversionFailedException;
2727
import org.springframework.core.convert.ConverterNotFoundException;
2828
import org.springframework.core.convert.TypeDescriptor;
29-
import org.springframework.core.convert.converter.Converter;
3029

3130
import static org.assertj.core.api.Assertions.assertThat;
3231
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -134,12 +133,7 @@ void convertFromListToStream() throws NoSuchFieldException {
134133
@SuppressWarnings("resource")
135134
void convertFromArrayToStream() throws NoSuchFieldException {
136135
Integer[] stream = new Integer[] {1, 0, 1};
137-
this.conversionService.addConverter(new Converter<Integer, Boolean>() {
138-
@Override
139-
public Boolean convert(Integer source) {
140-
return source == 1;
141-
}
142-
});
136+
this.conversionService.addConverter(Integer.class, Boolean.class, source -> source == 1);
143137
TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans"));
144138
Object result = this.conversionService.convert(stream, streamOfBoolean);
145139

spring-core/src/test/java/org/springframework/core/env/ProfilesTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ void ofWhenNullThrowsException() {
4747

4848
@Test
4949
void ofWhenEmptyThrowsException() {
50-
assertThatIllegalArgumentException().isThrownBy(() ->
51-
Profiles.of())
50+
assertThatIllegalArgumentException().isThrownBy(Profiles::of)
5251
.withMessageContaining("Must specify at least one profile");
5352
}
5453

spring-core/src/test/java/org/springframework/core/env/StandardEnvironmentTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void reservedDefaultProfile() {
212212
void defaultProfileWithCircularPlaceholder() {
213213
try {
214214
System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "${spring.profiles.default}");
215-
assertThatIllegalArgumentException().isThrownBy(() -> environment.getDefaultProfiles());
215+
assertThatIllegalArgumentException().isThrownBy(environment::getDefaultProfiles);
216216
}
217217
finally {
218218
System.clearProperty(DEFAULT_PROFILES_PROPERTY_NAME);

spring-core/src/test/java/org/springframework/core/io/buffer/DataBufferUtilsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void readInputStream(String displayName, DataBufferFactory bufferFactory) {
8080
super.bufferFactory = bufferFactory;
8181

8282
Flux<DataBuffer> flux = DataBufferUtils.readInputStream(
83-
() -> this.resource.getInputStream(), super.bufferFactory, 3);
83+
this.resource::getInputStream, super.bufferFactory, 3);
8484

8585
verifyReadData(flux);
8686
}

spring-core/src/test/java/org/springframework/core/task/SimpleAsyncTaskExecutorTests.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.core.task;
1818

19-
import java.util.concurrent.ThreadFactory;
20-
2119
import org.junit.jupiter.api.Test;
2220

2321
import org.springframework.util.ConcurrencyThrottleSupport;
@@ -61,12 +59,7 @@ void threadNameGetsSetCorrectly() throws Exception {
6159
@Test
6260
void threadFactoryOverridesDefaults() throws Exception {
6361
final Object monitor = new Object();
64-
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(new ThreadFactory() {
65-
@Override
66-
public Thread newThread(Runnable r) {
67-
return new Thread(r, "test");
68-
}
69-
});
62+
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(runnable -> new Thread(runnable, "test"));
7063
ThreadNameHarvester task = new ThreadNameHarvester(monitor);
7164
executeAndWait(executor, task, monitor);
7265
assertThat(task.getThreadName()).isEqualTo("test");

spring-core/src/test/java/org/springframework/util/concurrent/SettableListenableFutureTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,7 @@ void setExceptionPreventsCancel() {
332332
void cancelStateThrowsExceptionWhenCallingGet() throws ExecutionException, InterruptedException {
333333
settableListenableFuture.cancel(true);
334334

335-
assertThatExceptionOfType(CancellationException.class).isThrownBy(() ->
336-
settableListenableFuture.get());
335+
assertThatExceptionOfType(CancellationException.class).isThrownBy(settableListenableFuture::get);
337336

338337
assertThat(settableListenableFuture.isCancelled()).isTrue();
339338
assertThat(settableListenableFuture.isDone()).isTrue();

spring-core/src/test/java/org/springframework/util/xml/AbstractStaxXMLReaderTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,9 @@ protected final ContentHandler mockContentHandler() throws Exception {
179179
ContentHandler contentHandler = mock(ContentHandler.class);
180180
willAnswer(new CopyCharsAnswer()).given(contentHandler).characters(any(char[].class), anyInt(), anyInt());
181181
willAnswer(new CopyCharsAnswer()).given(contentHandler).ignorableWhitespace(any(char[].class), anyInt(), anyInt());
182-
willAnswer(new Answer<Object>() {
183-
@Override
184-
public Object answer(InvocationOnMock invocation) throws Throwable {
185-
invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
186-
return null;
187-
}
182+
willAnswer(invocation -> {
183+
invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
184+
return null;
188185
}).given(contentHandler).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
189186
return contentHandler;
190187
}

0 commit comments

Comments
 (0)