Browse Source

Specify generic type nullness in spring-test

See gh-34140
pull/34266/head
Sébastien Deleuze 1 year ago
parent
commit
e2216ddc32
  1. 4
      spring-test/src/main/java/org/springframework/test/context/web/WebMergedContextConfiguration.java
  2. 24
      spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java
  3. 16
      spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java
  4. 6
      spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcFilterDecorator.java

4
spring-test/src/main/java/org/springframework/test/context/web/WebMergedContextConfiguration.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -106,7 +106,7 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration { @@ -106,7 +106,7 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
* {@link #WebMergedContextConfiguration(Class, String[], Class[], Set, String[], List, String[], Set, String, ContextLoader, CacheAwareContextLoaderDelegate, MergedContextConfiguration)}
*/
@Deprecated(since = "6.1")
public WebMergedContextConfiguration(Class<?> testClass, String @Nullable [] locations, @Nullable Class<?>[] classes,
public WebMergedContextConfiguration(Class<?> testClass, String @Nullable [] locations, Class<?> @Nullable [] classes,
@Nullable Set<Class<? extends ApplicationContextInitializer<?>>> contextInitializerClasses,
String @Nullable [] activeProfiles, String @Nullable [] propertySourceLocations, String @Nullable [] propertySourceProperties,
String resourceBasePath, ContextLoader contextLoader,

24
spring-test/src/main/java/org/springframework/test/web/reactive/server/DefaultWebTestClient.java

@ -547,20 +547,21 @@ class DefaultWebTestClient implements WebTestClient { @@ -547,20 +547,21 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
public <T extends S> T isEqualTo(B expected) {
public <T extends S> T isEqualTo(@Nullable B expected) {
this.result.assertWithDiagnostics(() ->
AssertionErrors.assertEquals("Response body", expected, this.result.getResponseBody()));
return self();
}
@Override
public <T extends S> T value(Matcher<? super B> matcher) {
public <T extends S> T value(Matcher<? super @Nullable B> matcher) {
this.result.assertWithDiagnostics(() -> MatcherAssert.assertThat(this.result.getResponseBody(), matcher));
return self();
}
@Override
public <T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> matcher) {
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1129
public <T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher) {
this.result.assertWithDiagnostics(() -> {
B body = this.result.getResponseBody();
MatcherAssert.assertThat(bodyMapper.apply(body), matcher);
@ -569,7 +570,8 @@ class DefaultWebTestClient implements WebTestClient { @@ -569,7 +570,8 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
public <T extends S> T value(Consumer<B> consumer) {
@SuppressWarnings("NullAway") // https://github.com/uber/NullAway/issues/1129
public <T extends S> T value(Consumer<@Nullable B> consumer) {
this.result.assertWithDiagnostics(() -> consumer.accept(this.result.getResponseBody()));
return self();
}
@ -592,7 +594,7 @@ class DefaultWebTestClient implements WebTestClient { @@ -592,7 +594,7 @@ class DefaultWebTestClient implements WebTestClient {
}
private static class DefaultListBodySpec<E> extends DefaultBodySpec<List<E>, ListBodySpec<E>>
private static class DefaultListBodySpec<E> extends DefaultBodySpec<List<@Nullable E>, ListBodySpec<E>>
implements ListBodySpec<E> {
DefaultListBodySpec(EntityExchangeResult<List<E>> result) {
@ -601,7 +603,7 @@ class DefaultWebTestClient implements WebTestClient { @@ -601,7 +603,7 @@ class DefaultWebTestClient implements WebTestClient {
@Override
public ListBodySpec<E> hasSize(int size) {
List<E> actual = getResult().getResponseBody();
List<@Nullable E> actual = getResult().getResponseBody();
String message = "Response body does not contain " + size + " elements";
getResult().assertWithDiagnostics(() ->
AssertionErrors.assertEquals(message, size, (actual != null ? actual.size() : 0)));
@ -610,9 +612,9 @@ class DefaultWebTestClient implements WebTestClient { @@ -610,9 +612,9 @@ class DefaultWebTestClient implements WebTestClient {
@Override
@SuppressWarnings("unchecked")
public ListBodySpec<E> contains(E... elements) {
public ListBodySpec<E> contains(@Nullable E... elements) {
List<E> expected = Arrays.asList(elements);
List<E> actual = getResult().getResponseBody();
List<@Nullable E> actual = getResult().getResponseBody();
String message = "Response body does not contain " + expected;
getResult().assertWithDiagnostics(() ->
AssertionErrors.assertTrue(message, (actual != null && actual.containsAll(expected))));
@ -621,9 +623,9 @@ class DefaultWebTestClient implements WebTestClient { @@ -621,9 +623,9 @@ class DefaultWebTestClient implements WebTestClient {
@Override
@SuppressWarnings("unchecked")
public ListBodySpec<E> doesNotContain(E... elements) {
public ListBodySpec<E> doesNotContain(@Nullable E... elements) {
List<E> expected = Arrays.asList(elements);
List<E> actual = getResult().getResponseBody();
List<@Nullable E> actual = getResult().getResponseBody();
String message = "Response body should not have contained " + expected;
getResult().assertWithDiagnostics(() ->
AssertionErrors.assertTrue(message, (actual == null || !actual.containsAll(expected))));
@ -631,7 +633,7 @@ class DefaultWebTestClient implements WebTestClient { @@ -631,7 +633,7 @@ class DefaultWebTestClient implements WebTestClient {
}
@Override
public EntityExchangeResult<List<E>> returnResult() {
public EntityExchangeResult<List<@Nullable E>> returnResult() {
return getResult();
}
}

16
spring-test/src/main/java/org/springframework/test/web/reactive/server/WebTestClient.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -918,26 +918,26 @@ public interface WebTestClient { @@ -918,26 +918,26 @@ public interface WebTestClient {
/**
* Assert the extracted body is equal to the given value.
*/
<T extends S> T isEqualTo(B expected);
<T extends S> T isEqualTo(@Nullable B expected);
/**
* Assert the extracted body with a {@link Matcher}.
* @since 5.1
*/
<T extends S> T value(Matcher<? super B> matcher);
<T extends S> T value(Matcher<? super @Nullable B> matcher);
/**
* Transform the extracted the body with a function, for example, extracting a
* property, and assert the mapped value with a {@link Matcher}.
* @since 5.1
*/
<T extends S, R> T value(Function<B, R> bodyMapper, Matcher<? super R> matcher);
<T extends S, R> T value(Function<@Nullable B, @Nullable R> bodyMapper, Matcher<? super @Nullable R> matcher);
/**
* Assert the extracted body with a {@link Consumer}.
* @since 5.1
*/
<T extends S> T value(Consumer<B> consumer);
<T extends S> T value(Consumer<@Nullable B> consumer);
/**
* Assert the exchange result with the given {@link Consumer}.
@ -957,7 +957,7 @@ public interface WebTestClient { @@ -957,7 +957,7 @@ public interface WebTestClient {
*
* @param <E> the body list element type
*/
interface ListBodySpec<E> extends BodySpec<List<E>, ListBodySpec<E>> {
interface ListBodySpec<E> extends BodySpec<List<@Nullable E>, ListBodySpec<E>> {
/**
* Assert the extracted list of values is of the given size.
@ -970,14 +970,14 @@ public interface WebTestClient { @@ -970,14 +970,14 @@ public interface WebTestClient {
* @param elements the elements to check
*/
@SuppressWarnings("unchecked")
ListBodySpec<E> contains(E... elements);
ListBodySpec<E> contains(@Nullable E... elements);
/**
* Assert the extracted list of values doesn't contain the given elements.
* @param elements the elements to check
*/
@SuppressWarnings("unchecked")
ListBodySpec<E> doesNotContain(E... elements);
ListBodySpec<E> doesNotContain(@Nullable E... elements);
}

6
spring-test/src/main/java/org/springframework/test/web/servlet/setup/MockMvcFilterDecorator.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -58,7 +58,7 @@ final class MockMvcFilterDecorator implements Filter { @@ -58,7 +58,7 @@ final class MockMvcFilterDecorator implements Filter {
private final Filter delegate;
private final @Nullable Function<ServletContext, FilterConfig> filterConfigInitializer;
private final @Nullable Function<@Nullable ServletContext, FilterConfig> filterConfigInitializer;
private final @Nullable EnumSet<DispatcherType> dispatcherTypes;
@ -103,7 +103,7 @@ final class MockMvcFilterDecorator implements Filter { @@ -103,7 +103,7 @@ final class MockMvcFilterDecorator implements Filter {
this.hasPatterns = initPatterns(urlPatterns);
}
private static Function<ServletContext, FilterConfig> getFilterConfigInitializer(
private static Function<@Nullable ServletContext, FilterConfig> getFilterConfigInitializer(
Filter delegate, @Nullable String filterName, @Nullable Map<String, String> initParams) {
String className = delegate.getClass().getName();

Loading…
Cancel
Save