Refactor tests to use lambda expressions & method references

Closes gh-27386
This commit is contained in:
kth496 2021-09-11 14:40:24 +09:00 committed by Sam Brannen
parent 51d263bd06
commit 23ab9ca8ca
9 changed files with 11 additions and 34 deletions

View File

@ -257,7 +257,7 @@ class MissingMergedAnnotationTests {
@Test
void synthesizeThrowsNoSuchElementException() {
assertThatNoSuchElementException().isThrownBy(() -> this.missing.synthesize());
assertThatNoSuchElementException().isThrownBy(this.missing::synthesize);
}
@Test

View File

@ -190,12 +190,7 @@ class GenericConversionServiceTests {
@Test
void convertSuperSourceType() {
conversionService.addConverter(new Converter<CharSequence, Integer>() {
@Override
public Integer convert(CharSequence source) {
return Integer.valueOf(source.toString());
}
});
conversionService.addConverter(CharSequence.class, Integer.class, source -> Integer.valueOf(source.toString()));
Integer result = conversionService.convert("3", Integer.class);
assertThat((int) result).isEqualTo((int) Integer.valueOf(3));
}

View File

@ -26,7 +26,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.Converter;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@ -134,12 +133,7 @@ class StreamConverterTests {
@SuppressWarnings("resource")
void convertFromArrayToStream() throws NoSuchFieldException {
Integer[] stream = new Integer[] {1, 0, 1};
this.conversionService.addConverter(new Converter<Integer, Boolean>() {
@Override
public Boolean convert(Integer source) {
return source == 1;
}
});
this.conversionService.addConverter(Integer.class, Boolean.class, source -> source == 1);
TypeDescriptor streamOfBoolean = new TypeDescriptor(Types.class.getField("streamOfBooleans"));
Object result = this.conversionService.convert(stream, streamOfBoolean);

View File

@ -47,8 +47,7 @@ class ProfilesTests {
@Test
void ofWhenEmptyThrowsException() {
assertThatIllegalArgumentException().isThrownBy(() ->
Profiles.of())
assertThatIllegalArgumentException().isThrownBy(Profiles::of)
.withMessageContaining("Must specify at least one profile");
}

View File

@ -212,7 +212,7 @@ public class StandardEnvironmentTests {
void defaultProfileWithCircularPlaceholder() {
try {
System.setProperty(DEFAULT_PROFILES_PROPERTY_NAME, "${spring.profiles.default}");
assertThatIllegalArgumentException().isThrownBy(() -> environment.getDefaultProfiles());
assertThatIllegalArgumentException().isThrownBy(environment::getDefaultProfiles);
}
finally {
System.clearProperty(DEFAULT_PROFILES_PROPERTY_NAME);

View File

@ -80,7 +80,7 @@ class DataBufferUtilsTests extends AbstractDataBufferAllocatingTests {
super.bufferFactory = bufferFactory;
Flux<DataBuffer> flux = DataBufferUtils.readInputStream(
() -> this.resource.getInputStream(), super.bufferFactory, 3);
this.resource::getInputStream, super.bufferFactory, 3);
verifyReadData(flux);
}

View File

@ -16,8 +16,6 @@
package org.springframework.core.task;
import java.util.concurrent.ThreadFactory;
import org.junit.jupiter.api.Test;
import org.springframework.util.ConcurrencyThrottleSupport;
@ -61,12 +59,7 @@ class SimpleAsyncTaskExecutorTests {
@Test
void threadFactoryOverridesDefaults() throws Exception {
final Object monitor = new Object();
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "test");
}
});
SimpleAsyncTaskExecutor executor = new SimpleAsyncTaskExecutor(runnable -> new Thread(runnable, "test"));
ThreadNameHarvester task = new ThreadNameHarvester(monitor);
executeAndWait(executor, task, monitor);
assertThat(task.getThreadName()).isEqualTo("test");

View File

@ -332,8 +332,7 @@ class SettableListenableFutureTests {
void cancelStateThrowsExceptionWhenCallingGet() throws ExecutionException, InterruptedException {
settableListenableFuture.cancel(true);
assertThatExceptionOfType(CancellationException.class).isThrownBy(() ->
settableListenableFuture.get());
assertThatExceptionOfType(CancellationException.class).isThrownBy(settableListenableFuture::get);
assertThat(settableListenableFuture.isCancelled()).isTrue();
assertThat(settableListenableFuture.isDone()).isTrue();

View File

@ -179,12 +179,9 @@ abstract class AbstractStaxXMLReaderTests {
ContentHandler contentHandler = mock(ContentHandler.class);
willAnswer(new CopyCharsAnswer()).given(contentHandler).characters(any(char[].class), anyInt(), anyInt());
willAnswer(new CopyCharsAnswer()).given(contentHandler).ignorableWhitespace(any(char[].class), anyInt(), anyInt());
willAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
return null;
}
willAnswer(invocation -> {
invocation.getArguments()[3] = new AttributesImpl((Attributes) invocation.getArguments()[3]);
return null;
}).given(contentHandler).startElement(anyString(), anyString(), anyString(), any(Attributes.class));
return contentHandler;
}