Skip to content

Commit e2216dd

Browse files
committed
Specify generic type nullness in spring-test
See spring-projectsgh-34140
1 parent 5be36ce commit e2216dd

File tree

4 files changed

+26
-24
lines changed

4 files changed

+26
-24
lines changed

Diff for: spring-test/src/main/java/org/springframework/test/context/web/WebMergedContextConfiguration.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -106,7 +106,7 @@ public WebMergedContextConfiguration(MergedContextConfiguration mergedConfig, St
106106
* {@link #WebMergedContextConfiguration(Class, String[], Class[], Set, String[], List, String[], Set, String, ContextLoader, CacheAwareContextLoaderDelegate, MergedContextConfiguration)}
107107
*/
108108
@Deprecated(since = "6.1")
109-
public WebMergedContextConfiguration(Class<?> testClass, String @Nullable [] locations, @Nullable Class<?>[] classes,
109+
public WebMergedContextConfiguration(Class<?> testClass, String @Nullable [] locations, Class<?> @Nullable [] classes,
110110
@Nullable Set<Class<? extends ApplicationContextInitializer<?>>> contextInitializerClasses,
111111
String @Nullable [] activeProfiles, String @Nullable [] propertySourceLocations, String @Nullable [] propertySourceProperties,
112112
String resourceBasePath, ContextLoader contextLoader,

Diff for: spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -547,20 +547,21 @@ protected EntityExchangeResult<B> getResult() {
547547
}
548548

549549
@Override
550-
public <T extends S> T isEqualTo(B expected) {
550+
public <T extends S> T isEqualTo(@Nullable B expected) {
551551
this.result.assertWithDiagnostics(() ->
552552
AssertionErrors.assertEquals("Response body", expected, this.result.getResponseBody()));
553553
return self();
554554
}
555555

556556
@Override
557-
public <T extends S> T value(Matcher<? super B> matcher) {
557+
public <T extends S> T value(Matcher<? super @Nullable B> matcher) {
558558
this.result.assertWithDiagnostics(() -> MatcherAssert.assertThat(this.result.getResponseBody(), matcher));
559559
return self();
560560
}
561561

562562
@Override
563-
public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> matcher) {
563+
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1129
564+
public <T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher) {
564565
this.result.assertWithDiagnostics(() -> {
565566
B body = this.result.getResponseBody();
566567
MatcherAssert.assertThat(bodyMapper.apply(body), matcher);
@@ -569,7 +570,8 @@ public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> ma
569570
}
570571

571572
@Override
572-
public <T extends S> T value(Consumer<B> consumer) {
573+
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1129
574+
public <T extends S> T value(Consumer<@Nullable B> consumer) {
573575
this.result.assertWithDiagnostics(() -> consumer.accept(this.result.getResponseBody()));
574576
return self();
575577
}
@@ -592,7 +594,7 @@ public EntityExchangeResult<B> returnResult() {
592594
}
593595

594596

595-
private static class DefaultListBodySpec<E> extends DefaultBodySpec<List<E>, ListBodySpec<E>>
597+
private static class DefaultListBodySpec<E> extends DefaultBodySpec<List<@Nullable E>, ListBodySpec<E>>
596598
implements ListBodySpec<E> {
597599

598600
DefaultListBodySpec(EntityExchangeResult<List<E>> result) {
@@ -601,7 +603,7 @@ private static class DefaultListBodySpec<E> extends DefaultBodySpec<List<E>, Lis
601603

602604
@Override
603605
public ListBodySpec<E> hasSize(int size) {
604-
List<E> actual = getResult().getResponseBody();
606+
List<@Nullable E> actual = getResult().getResponseBody();
605607
String message = "Response body does not contain " + size + " elements";
606608
getResult().assertWithDiagnostics(() ->
607609
AssertionErrors.assertEquals(message, size, (actual != null ? actual.size() : 0)));
@@ -610,9 +612,9 @@ public ListBodySpec<E> hasSize(int size) {
610612

611613
@Override
612614
@SuppressWarnings("unchecked")
613-
public ListBodySpec<E> contains(E... elements) {
615+
public ListBodySpec<E> contains(@Nullable E... elements) {
614616
List<E> expected = Arrays.asList(elements);
615-
List<E> actual = getResult().getResponseBody();
617+
List<@Nullable E> actual = getResult().getResponseBody();
616618
String message = "Response body does not contain " + expected;
617619
getResult().assertWithDiagnostics(() ->
618620
AssertionErrors.assertTrue(message, (actual != null && actual.containsAll(expected))));
@@ -621,17 +623,17 @@ public ListBodySpec<E> contains(E... elements) {
621623

622624
@Override
623625
@SuppressWarnings("unchecked")
624-
public ListBodySpec<E> doesNotContain(E... elements) {
626+
public ListBodySpec<E> doesNotContain(@Nullable E... elements) {
625627
List<E> expected = Arrays.asList(elements);
626-
List<E> actual = getResult().getResponseBody();
628+
List<@Nullable E> actual = getResult().getResponseBody();
627629
String message = "Response body should not have contained " + expected;
628630
getResult().assertWithDiagnostics(() ->
629631
AssertionErrors.assertTrue(message, (actual == null || !actual.containsAll(expected))));
630632
return this;
631633
}
632634

633635
@Override
634-
public EntityExchangeResult<List<E>> returnResult() {
636+
public EntityExchangeResult<List<@Nullable E>> returnResult() {
635637
return getResult();
636638
}
637639
}

Diff for: spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -918,26 +918,26 @@ interface BodySpec<B, S extends BodySpec<B, S>> {
918918
/**
919919
* Assert the extracted body is equal to the given value.
920920
*/
921-
<T extends S> T isEqualTo(B expected);
921+
<T extends S> T isEqualTo(@Nullable B expected);
922922

923923
/**
924924
* Assert the extracted body with a {@link Matcher}.
925925
* @since 5.1
926926
*/
927-
<T extends S> T value(Matcher<? super B> matcher);
927+
<T extends S> T value(Matcher<? super @Nullable B> matcher);
928928

929929
/**
930930
* Transform the extracted the body with a function, for example, extracting a
931931
* property, and assert the mapped value with a {@link Matcher}.
932932
* @since 5.1
933933
*/
934-
<T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> matcher);
934+
<T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher);
935935

936936
/**
937937
* Assert the extracted body with a {@link Consumer}.
938938
* @since 5.1
939939
*/
940-
<T extends S> T value(Consumer<B> consumer);
940+
<T extends S> T value(Consumer<@Nullable B> consumer);
941941

942942
/**
943943
* Assert the exchange result with the given {@link Consumer}.
@@ -957,7 +957,7 @@ interface BodySpec<B, S extends BodySpec<B, S>> {
957957
*
958958
* @param <E> the body list element type
959959
*/
960-
interface ListBodySpec<E> extends BodySpec<List<E>, ListBodySpec<E>> {
960+
interface ListBodySpec<E> extends BodySpec<List<@Nullable E>, ListBodySpec<E>> {
961961

962962
/**
963963
* Assert the extracted list of values is of the given size.
@@ -970,14 +970,14 @@ interface ListBodySpec<E> extends BodySpec<List<E>, ListBodySpec<E>> {
970970
* @param elements the elements to check
971971
*/
972972
@SuppressWarnings("unchecked")
973-
ListBodySpec<E> contains(E... elements);
973+
ListBodySpec<E> contains(@Nullable E... elements);
974974

975975
/**
976976
* Assert the extracted list of values doesn't contain the given elements.
977977
* @param elements the elements to check
978978
*/
979979
@SuppressWarnings("unchecked")
980-
ListBodySpec<E> doesNotContain(E... elements);
980+
ListBodySpec<E> doesNotContain(@Nullable E... elements);
981981
}
982982

983983

Diff for: spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcFilterDecorator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -58,7 +58,7 @@ final class MockMvcFilterDecorator implements Filter {
5858

5959
private final Filter delegate;
6060

61-
private final @Nullable Function<ServletContext, FilterConfig> filterConfigInitializer;
61+
private final @Nullable Function<@Nullable ServletContext, FilterConfig> filterConfigInitializer;
6262

6363
private final @Nullable EnumSet<DispatcherType> dispatcherTypes;
6464

@@ -103,7 +103,7 @@ public MockMvcFilterDecorator(
103103
this.hasPatterns = initPatterns(urlPatterns);
104104
}
105105

106-
private static Function<ServletContext, FilterConfig> getFilterConfigInitializer(
106+
private static Function<@Nullable ServletContext, FilterConfig> getFilterConfigInitializer(
107107
Filter delegate, @Nullable String filterName, @Nullable Map<String, String> initParams) {
108108

109109
String className = delegate.getClass().getName();

0 commit comments

Comments
 (0)