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 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -106,7 +106,7 @@ public class WebMergedContextConfiguration extends MergedContextConfiguration {
* {@link #WebMergedContextConfiguration(Class, String[], Class[], Set, String[], List, String[], Set, String, ContextLoader, CacheAwareContextLoaderDelegate, MergedContextConfiguration)} * {@link #WebMergedContextConfiguration(Class, String[], Class[], Set, String[], List, String[], Set, String, ContextLoader, CacheAwareContextLoaderDelegate, MergedContextConfiguration)}
*/ */
@Deprecated(since = "6.1") @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, @Nullable Set<Class<? extends ApplicationContextInitializer<?>>> contextInitializerClasses,
String @Nullable [] activeProfiles, String @Nullable [] propertySourceLocations, String @Nullable [] propertySourceProperties, String @Nullable [] activeProfiles, String @Nullable [] propertySourceLocations, String @Nullable [] propertySourceProperties,
String resourceBasePath, ContextLoader contextLoader, 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 {
} }
@Override @Override
public <T extends S> T isEqualTo(B expected) { public <T extends S> T isEqualTo(@Nullable B expected) {
this.result.assertWithDiagnostics(() -> this.result.assertWithDiagnostics(() ->
AssertionErrors.assertEquals("Response body", expected, this.result.getResponseBody())); AssertionErrors.assertEquals("Response body", expected, this.result.getResponseBody()));
return self(); return self();
} }
@Override @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)); this.result.assertWithDiagnostics(() -> MatcherAssert.assertThat(this.result.getResponseBody(), matcher));
return self(); return self();
} }
@Override @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(() -> { this.result.assertWithDiagnostics(() -> {
B body = this.result.getResponseBody(); B body = this.result.getResponseBody();
MatcherAssert.assertThat(bodyMapper.apply(body), matcher); MatcherAssert.assertThat(bodyMapper.apply(body), matcher);
@ -569,7 +570,8 @@ class DefaultWebTestClient implements WebTestClient {
} }
@Override @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())); this.result.assertWithDiagnostics(() -> consumer.accept(this.result.getResponseBody()));
return self(); return self();
} }
@ -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> { implements ListBodySpec<E> {
DefaultListBodySpec(EntityExchangeResult<List<E>> result) { DefaultListBodySpec(EntityExchangeResult<List<E>> result) {
@ -601,7 +603,7 @@ class DefaultWebTestClient implements WebTestClient {
@Override @Override
public ListBodySpec<E> hasSize(int size) { 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"; String message = "Response body does not contain " + size + " elements";
getResult().assertWithDiagnostics(() -> getResult().assertWithDiagnostics(() ->
AssertionErrors.assertEquals(message, size, (actual != null ? actual.size() : 0))); AssertionErrors.assertEquals(message, size, (actual != null ? actual.size() : 0)));
@ -610,9 +612,9 @@ class DefaultWebTestClient implements WebTestClient {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ListBodySpec<E> contains(E... elements) { public ListBodySpec<E> contains(@Nullable E... elements) {
List<E> expected = Arrays.asList(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; String message = "Response body does not contain " + expected;
getResult().assertWithDiagnostics(() -> getResult().assertWithDiagnostics(() ->
AssertionErrors.assertTrue(message, (actual != null && actual.containsAll(expected)))); AssertionErrors.assertTrue(message, (actual != null && actual.containsAll(expected))));
@ -621,9 +623,9 @@ class DefaultWebTestClient implements WebTestClient {
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public ListBodySpec<E> doesNotContain(E... elements) { public ListBodySpec<E> doesNotContain(@Nullable E... elements) {
List<E> expected = Arrays.asList(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; String message = "Response body should not have contained " + expected;
getResult().assertWithDiagnostics(() -> getResult().assertWithDiagnostics(() ->
AssertionErrors.assertTrue(message, (actual == null || !actual.containsAll(expected)))); AssertionErrors.assertTrue(message, (actual == null || !actual.containsAll(expected))));
@ -631,7 +633,7 @@ class DefaultWebTestClient implements WebTestClient {
} }
@Override @Override
public EntityExchangeResult<List<E>> returnResult() { public EntityExchangeResult<List<@Nullable E>> returnResult() {
return getResult(); return getResult();
} }
} }

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

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -918,26 +918,26 @@ public interface WebTestClient {
/** /**
* Assert the extracted body is equal to the given value. * 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}. * Assert the extracted body with a {@link Matcher}.
* @since 5.1 * @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 * Transform the extracted the body with a function, for example, extracting a
* property, and assert the mapped value with a {@link Matcher}. * property, and assert the mapped value with a {@link Matcher}.
* @since 5.1 * @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}. * Assert the extracted body with a {@link Consumer}.
* @since 5.1 * @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}. * Assert the exchange result with the given {@link Consumer}.
@ -957,7 +957,7 @@ public interface WebTestClient {
* *
* @param <E> the body list element type * @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. * Assert the extracted list of values is of the given size.
@ -970,14 +970,14 @@ public interface WebTestClient {
* @param elements the elements to check * @param elements the elements to check
*/ */
@SuppressWarnings("unchecked") @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. * Assert the extracted list of values doesn't contain the given elements.
* @param elements the elements to check * @param elements the elements to check
*/ */
@SuppressWarnings("unchecked") @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 @@
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -58,7 +58,7 @@ final class MockMvcFilterDecorator implements Filter {
private final Filter delegate; 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; private final @Nullable EnumSet<DispatcherType> dispatcherTypes;
@ -103,7 +103,7 @@ final class MockMvcFilterDecorator implements Filter {
this.hasPatterns = initPatterns(urlPatterns); 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) { Filter delegate, @Nullable String filterName, @Nullable Map<String, String> initParams) {
String className = delegate.getClass().getName(); String className = delegate.getClass().getName();

Loading…
Cancel
Save