Skip to content

Commit 23c2f73

Browse files
committed
Update tests to allow them to run on Java 19
Closes gh-32280
1 parent 78f4242 commit 23c2f73

File tree

10 files changed

+182
-53
lines changed

10 files changed

+182
-53
lines changed

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/management/ThreadDumpEndpointTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,7 +105,11 @@ void dumpThreadsAsText() throws InterruptedException {
105105
.contains(String.format("\t- waiting to lock <%s> (a java.lang.Object) owned by \"%s\" t@%d",
106106
hexIdentityHashCode(contendedMonitor), Thread.currentThread().getName(),
107107
Thread.currentThread().getId()))
108-
.contains(String.format("\t- waiting on <%s> (a java.lang.Object)", hexIdentityHashCode(monitor)))
108+
.satisfiesAnyOf(
109+
(dump) -> dump.contains(String.format("\t- waiting on <%s> (a java.lang.Object)",
110+
hexIdentityHashCode(monitor))),
111+
(dump) -> dump.contains(String.format("\t- parking to wait for <%s> (a java.lang.Object)",
112+
hexIdentityHashCode(monitor))))
109113
.containsPattern(
110114
String.format("Locked ownable synchronizers:%n\t- Locked <[0-9a-z]+> \\(a %s\\$NonfairSync\\)",
111115
ReentrantReadWriteLock.class.getName().replace(".", "\\.")));

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/ReactiveWebServerFactoryCustomizerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void testCustomizeServerPort() {
5959
@Test
6060
void testCustomizeServerAddress() {
6161
ConfigurableReactiveWebServerFactory factory = mock(ConfigurableReactiveWebServerFactory.class);
62-
InetAddress address = mock(InetAddress.class);
62+
InetAddress address = InetAddress.getLoopbackAddress();
6363
this.properties.setAddress(address);
6464
this.customizer.customize(factory);
6565
then(factory).should().setAddress(address);

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,7 @@ task zip(type: Zip) {
120120
artifacts {
121121
"documentation" zip
122122
}
123+
124+
toolchain {
125+
maximumCompatibleJavaVersion = JavaLanguageVersion.of(18)
126+
}

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import java.util.Set;
2222

2323
import org.junit.jupiter.api.Test;
24-
import org.mockito.Answers;
2524
import org.mockito.InOrder;
25+
import org.mockito.invocation.InvocationOnMock;
26+
import org.mockito.stubbing.Answer;
2627

2728
import org.springframework.boot.context.properties.source.ConfigurationProperty;
2829
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
@@ -35,6 +36,7 @@
3536
import static org.mockito.ArgumentMatchers.any;
3637
import static org.mockito.ArgumentMatchers.eq;
3738
import static org.mockito.ArgumentMatchers.isA;
39+
import static org.mockito.BDDMockito.given;
3840
import static org.mockito.Mockito.inOrder;
3941
import static org.mockito.Mockito.mock;
4042

@@ -68,7 +70,7 @@ void bindToArrayShouldReturnArray() {
6870
@Test
6971
void bindToCollectionShouldTriggerOnSuccess() {
7072
this.sources.add(new MockConfigurationPropertySource("foo[0]", "1", "line1"));
71-
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
73+
BindHandler handler = mockBindHandler();
7274
this.binder.bind("foo", INTEGER_LIST, handler);
7375
InOrder inOrder = inOrder(handler);
7476
inOrder.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo[0]")), eq(Bindable.of(Integer.class)),
@@ -198,7 +200,7 @@ void bindToArrayWhenNoValueShouldReturnUnbound() {
198200
@Test
199201
void bindToArrayShouldTriggerOnSuccess() {
200202
this.sources.add(new MockConfigurationPropertySource("foo[0]", "1", "line1"));
201-
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
203+
BindHandler handler = mockBindHandler();
202204
Bindable<Integer[]> target = INTEGER_ARRAY;
203205
this.binder.bind("foo", target, handler);
204206
InOrder inOrder = inOrder(handler);
@@ -285,4 +287,31 @@ void bindToArrayWhenStringShouldUsePropertyEditor() {
285287
IllegalStateException.class);
286288
}
287289

290+
private BindHandler mockBindHandler() {
291+
BindHandler handler = mock(BindHandler.class);
292+
given(handler.onStart(any(), any(), any())).willAnswer(InvocationArgument.index(1));
293+
given(handler.onCreate(any(), any(), any(), any())).willAnswer(InvocationArgument.index(3));
294+
given(handler.onSuccess(any(), any(), any(), any())).willAnswer(InvocationArgument.index(3));
295+
return handler;
296+
}
297+
298+
private static final class InvocationArgument<T> implements Answer<T> {
299+
300+
private final int index;
301+
302+
private InvocationArgument(int index) {
303+
this.index = index;
304+
}
305+
306+
@Override
307+
public T answer(InvocationOnMock invocation) throws Throwable {
308+
return invocation.getArgument(this.index);
309+
}
310+
311+
private static <T> InvocationArgument<T> index(int index) {
312+
return new InvocationArgument<>(index);
313+
}
314+
315+
}
316+
288317
}

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

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
import javax.validation.Validation;
2828

2929
import org.junit.jupiter.api.Test;
30-
import org.mockito.Answers;
3130
import org.mockito.InOrder;
31+
import org.mockito.invocation.InvocationOnMock;
32+
import org.mockito.stubbing.Answer;
3233

3334
import org.springframework.boot.context.properties.bind.Bindable.BindRestriction;
3435
import org.springframework.boot.context.properties.bind.validation.ValidationBindHandler;
@@ -54,6 +55,7 @@
5455
import static org.mockito.ArgumentMatchers.any;
5556
import static org.mockito.ArgumentMatchers.eq;
5657
import static org.mockito.ArgumentMatchers.isA;
58+
import static org.mockito.BDDMockito.given;
5759
import static org.mockito.Mockito.inOrder;
5860
import static org.mockito.Mockito.mock;
5961

@@ -174,7 +176,7 @@ void bindToValueWithCustomPropertyEditorShouldReturnConvertedValue() {
174176
@Test
175177
void bindToValueShouldTriggerOnSuccess() {
176178
this.sources.add(new MockConfigurationPropertySource("foo", "1", "line1"));
177-
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
179+
BindHandler handler = mockBindHandler();
178180
Bindable<Integer> target = Bindable.of(Integer.class);
179181
this.binder.bind("foo", target, handler);
180182
InOrder ordered = inOrder(handler);
@@ -183,7 +185,7 @@ void bindToValueShouldTriggerOnSuccess() {
183185

184186
@Test
185187
void bindOrCreateWhenNotBoundShouldTriggerOnCreate() {
186-
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
188+
BindHandler handler = mock(BindHandler.class);
187189
Bindable<JavaBean> target = Bindable.of(JavaBean.class);
188190
this.binder.bindOrCreate("foo", target, handler);
189191
InOrder ordered = inOrder(handler);
@@ -219,7 +221,7 @@ void bindToJavaBeanWhenHasPropertyWithSameNameShouldStillBind() {
219221
@Test
220222
void bindToJavaBeanShouldTriggerOnSuccess() {
221223
this.sources.add(new MockConfigurationPropertySource("foo.value", "bar", "line1"));
222-
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
224+
BindHandler handler = mockBindHandler();
223225
Bindable<JavaBean> target = Bindable.of(JavaBean.class);
224226
this.binder.bind("foo", target, handler);
225227
InOrder inOrder = inOrder(handler);
@@ -232,7 +234,7 @@ void bindToJavaBeanShouldTriggerOnSuccess() {
232234
@Test
233235
void bindWhenHasCustomDefaultHandlerShouldTriggerOnSuccess() {
234236
this.sources.add(new MockConfigurationPropertySource("foo.value", "bar", "line1"));
235-
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
237+
BindHandler handler = mockBindHandler();
236238
Binder binder = new Binder(this.sources, null, null, null, handler);
237239
Bindable<JavaBean> target = Bindable.of(JavaBean.class);
238240
binder.bind("foo", target);
@@ -359,6 +361,14 @@ private JavaBeanWithPublicConstructor bindToJavaBeanWithPublicConstructor(
359361
return this.binder.bindOrCreate("foo", bindable);
360362
}
361363

364+
private BindHandler mockBindHandler() {
365+
BindHandler handler = mock(BindHandler.class);
366+
given(handler.onStart(any(), any(), any())).willAnswer(InvocationArgument.index(1));
367+
given(handler.onCreate(any(), any(), any(), any())).willAnswer(InvocationArgument.index(3));
368+
given(handler.onSuccess(any(), any(), any(), any())).willAnswer(InvocationArgument.index(3));
369+
return handler;
370+
}
371+
362372
static class JavaBean {
363373

364374
private String value;
@@ -487,4 +497,23 @@ public void setAsText(String text) {
487497

488498
}
489499

500+
private static final class InvocationArgument<T> implements Answer<T> {
501+
502+
private final int index;
503+
504+
private InvocationArgument(int index) {
505+
this.index = index;
506+
}
507+
508+
@Override
509+
public T answer(InvocationOnMock invocation) throws Throwable {
510+
return invocation.getArgument(this.index);
511+
}
512+
513+
private static <T> InvocationArgument<T> index(int index) {
514+
return new InvocationArgument<>(index);
515+
}
516+
517+
}
518+
490519
}

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
import java.util.stream.Collectors;
2828

2929
import org.junit.jupiter.api.Test;
30-
import org.mockito.Answers;
3130
import org.mockito.ArgumentCaptor;
3231
import org.mockito.InOrder;
32+
import org.mockito.invocation.InvocationOnMock;
33+
import org.mockito.stubbing.Answer;
3334

3435
import org.springframework.boot.context.properties.bind.BinderTests.ExampleEnum;
3536
import org.springframework.boot.context.properties.bind.BinderTests.JavaBean;
@@ -50,6 +51,7 @@
5051
import static org.mockito.ArgumentMatchers.any;
5152
import static org.mockito.ArgumentMatchers.eq;
5253
import static org.mockito.ArgumentMatchers.isA;
54+
import static org.mockito.BDDMockito.given;
5355
import static org.mockito.Mockito.inOrder;
5456
import static org.mockito.Mockito.mock;
5557

@@ -329,7 +331,7 @@ void bindToMapWithNoPropertiesShouldReturnUnbound() {
329331
@Test
330332
void bindToMapShouldTriggerOnSuccess() {
331333
this.sources.add(new MockConfigurationPropertySource("foo.bar", "1", "line1"));
332-
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
334+
BindHandler handler = mockBindHandler();
333335
Bindable<Map<String, Integer>> target = STRING_INTEGER_MAP;
334336
this.binder.bind("foo", target, handler);
335337
InOrder ordered = inOrder(handler);
@@ -341,7 +343,7 @@ void bindToMapShouldTriggerOnSuccess() {
341343
@Test
342344
void bindToMapStringArrayShouldTriggerOnSuccess() {
343345
this.sources.add(new MockConfigurationPropertySource("foo.bar", "a,b,c", "line1"));
344-
BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
346+
BindHandler handler = mockBindHandler();
345347
Bindable<Map<String, String[]>> target = STRING_ARRAY_MAP;
346348
this.binder.bind("foo", target, handler);
347349
InOrder ordered = inOrder(handler);
@@ -614,6 +616,14 @@ private <T> Bindable<List<T>> getListBindable(ResolvableType type) {
614616
return Bindable.of(ResolvableType.forClassWithGenerics(List.class, type));
615617
}
616618

619+
private BindHandler mockBindHandler() {
620+
BindHandler handler = mock(BindHandler.class);
621+
given(handler.onStart(any(), any(), any())).willAnswer(InvocationArgument.index(1));
622+
given(handler.onCreate(any(), any(), any(), any())).willAnswer(InvocationArgument.index(3));
623+
given(handler.onSuccess(any(), any(), any(), any())).willAnswer(InvocationArgument.index(3));
624+
return handler;
625+
}
626+
617627
static class Foo {
618628

619629
private String pattern;
@@ -734,4 +744,23 @@ void setAddresses(Map<String, ? extends List<? extends InetAddress>> addresses)
734744

735745
}
736746

747+
private static final class InvocationArgument<T> implements Answer<T> {
748+
749+
private final int index;
750+
751+
private InvocationArgument(int index) {
752+
this.index = index;
753+
}
754+
755+
@Override
756+
public T answer(InvocationOnMock invocation) throws Throwable {
757+
return invocation.getArgument(this.index);
758+
}
759+
760+
private static <T> InvocationArgument<T> index(int index) {
761+
return new InvocationArgument<>(index);
762+
}
763+
764+
}
765+
737766
}

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
import java.util.Collections;
2020

2121
import org.junit.jupiter.api.Test;
22-
import org.mockito.Answers;
2322

2423
import static org.assertj.core.api.Assertions.assertThat;
25-
import static org.mockito.BDDMockito.given;
26-
import static org.mockito.Mockito.mock;
2724

2825
/**
2926
* Tests for {@link AliasedConfigurationPropertySource}.
@@ -57,8 +54,7 @@ void getConfigurationPropertyWhenNotAliasesShouldReturnValue() {
5754
@Test
5855
void containsDescendantOfWhenSourceReturnsUnknownShouldReturnUnknown() {
5956
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
60-
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class, Answers.CALLS_REAL_METHODS);
61-
given(source.containsDescendantOf(name)).willReturn(ConfigurationPropertyState.UNKNOWN);
57+
ConfigurationPropertySource source = new KnownAncestorsConfigurationPropertySource().unknown(name);
6258
ConfigurationPropertySource aliased = source
6359
.withAliases(new ConfigurationPropertyNameAliases("foo.bar", "foo.bar1"));
6460
assertThat(aliased.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.UNKNOWN);
@@ -67,10 +63,8 @@ void containsDescendantOfWhenSourceReturnsUnknownShouldReturnUnknown() {
6763
@Test
6864
void containsDescendantOfWhenSourceReturnsPresentShouldReturnPresent() {
6965
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
70-
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class, Answers.CALLS_REAL_METHODS);
71-
given(source.containsDescendantOf(name)).willReturn(ConfigurationPropertyState.PRESENT);
72-
given(source.containsDescendantOf(ConfigurationPropertyName.of("bar")))
73-
.willReturn(ConfigurationPropertyState.UNKNOWN);
66+
ConfigurationPropertySource source = new KnownAncestorsConfigurationPropertySource().present(name)
67+
.unknown(ConfigurationPropertyName.of("bar"));
7468
ConfigurationPropertySource aliased = source
7569
.withAliases(new ConfigurationPropertyNameAliases("foo.bar", "foo.bar1"));
7670
assertThat(aliased.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.PRESENT);
@@ -79,21 +73,17 @@ void containsDescendantOfWhenSourceReturnsPresentShouldReturnPresent() {
7973
@Test
8074
void containsDescendantOfWhenAllAreAbsentShouldReturnAbsent() {
8175
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
82-
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class, Answers.CALLS_REAL_METHODS);
83-
given(source.containsDescendantOf(name)).willReturn(ConfigurationPropertyState.ABSENT);
84-
given(source.containsDescendantOf(ConfigurationPropertyName.of("bar")))
85-
.willReturn(ConfigurationPropertyState.ABSENT);
76+
ConfigurationPropertySource source = new KnownAncestorsConfigurationPropertySource().absent(name)
77+
.absent(ConfigurationPropertyName.of("bar"));
8678
ConfigurationPropertySource aliased = source.withAliases(new ConfigurationPropertyNameAliases("foo", "bar"));
8779
assertThat(aliased.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.ABSENT);
8880
}
8981

9082
@Test
9183
void containsDescendantOfWhenAnyIsPresentShouldReturnPresent() {
9284
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
93-
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class, Answers.CALLS_REAL_METHODS);
94-
given(source.containsDescendantOf(name)).willReturn(ConfigurationPropertyState.ABSENT);
95-
given(source.containsDescendantOf(ConfigurationPropertyName.of("bar")))
96-
.willReturn(ConfigurationPropertyState.PRESENT);
85+
ConfigurationPropertySource source = new KnownAncestorsConfigurationPropertySource().absent(name)
86+
.present(ConfigurationPropertyName.of("bar"));
9787
ConfigurationPropertySource aliased = source.withAliases(new ConfigurationPropertyNameAliases("foo", "bar"));
9888
assertThat(aliased.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.PRESENT);
9989
}

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,9 @@
1919
import java.util.Objects;
2020

2121
import org.junit.jupiter.api.Test;
22-
import org.mockito.Answers;
2322

2423
import static org.assertj.core.api.Assertions.assertThat;
2524
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
26-
import static org.mockito.BDDMockito.given;
27-
import static org.mockito.Mockito.mock;
2825

2926
/**
3027
* Test for {@link FilteredIterableConfigurationPropertiesSource}.
@@ -64,26 +61,23 @@ void getValueShouldFilterNames() {
6461
@Test
6562
void containsDescendantOfWhenSourceReturnsEmptyShouldReturnEmpty() {
6663
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
67-
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class, Answers.CALLS_REAL_METHODS);
68-
given(source.containsDescendantOf(name)).willReturn(ConfigurationPropertyState.UNKNOWN);
64+
ConfigurationPropertySource source = new KnownAncestorsConfigurationPropertySource().unknown(name);
6965
ConfigurationPropertySource filtered = source.filter((n) -> true);
7066
assertThat(filtered.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.UNKNOWN);
7167
}
7268

7369
@Test
7470
void containsDescendantOfWhenSourceReturnsFalseShouldReturnFalse() {
7571
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
76-
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class, Answers.CALLS_REAL_METHODS);
77-
given(source.containsDescendantOf(name)).willReturn(ConfigurationPropertyState.ABSENT);
72+
ConfigurationPropertySource source = new KnownAncestorsConfigurationPropertySource().absent(name);
7873
ConfigurationPropertySource filtered = source.filter((n) -> true);
7974
assertThat(filtered.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.ABSENT);
8075
}
8176

8277
@Test
8378
void containsDescendantOfWhenSourceReturnsTrueShouldReturnEmpty() {
8479
ConfigurationPropertyName name = ConfigurationPropertyName.of("foo");
85-
ConfigurationPropertySource source = mock(ConfigurationPropertySource.class, Answers.CALLS_REAL_METHODS);
86-
given(source.containsDescendantOf(name)).willReturn(ConfigurationPropertyState.PRESENT);
80+
ConfigurationPropertySource source = new KnownAncestorsConfigurationPropertySource().present(name);
8781
ConfigurationPropertySource filtered = source.filter((n) -> true);
8882
assertThat(filtered.containsDescendantOf(name)).isEqualTo(ConfigurationPropertyState.UNKNOWN);
8983
}

0 commit comments

Comments
 (0)