Skip to content

Commit 80c384e

Browse files
committed
Add nullability annotations to tests in module/spring-boot-health
See gh-47263
1 parent 0634c11 commit 80c384e

18 files changed

+93
-28
lines changed

module/spring-boot-health/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ dependencies {
3737
testImplementation("io.projectreactor:reactor-test")
3838
testImplementation("tools.jackson.core:jackson-databind")
3939

40+
testCompileOnly("com.google.code.findbugs:jsr305")
41+
4042
testRuntimeOnly("ch.qos.logback:logback-classic")
4143
}
44+
45+
tasks.named("compileTestJava") {
46+
options.nullability.checking = "tests"
47+
}

module/spring-boot-health/src/test/java/org/springframework/boot/health/autoconfigure/contributor/AbstractCompositeHealthContributorConfigurationTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Map;
2222
import java.util.stream.Stream;
2323

24+
import org.jspecify.annotations.Nullable;
2425
import org.junit.jupiter.api.Test;
2526

2627
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -41,7 +42,7 @@
4142
*/
4243
abstract class AbstractCompositeHealthContributorConfigurationTests<C, I extends C> {
4344

44-
private final Class<?> indicatorType;
45+
private final @Nullable Class<?> indicatorType;
4546

4647
AbstractCompositeHealthContributorConfigurationTests() {
4748
ResolvableType type = ResolvableType.forClass(AbstractCompositeHealthContributorConfigurationTests.class,

module/spring-boot-health/src/test/java/org/springframework/boot/health/contributor/CompositeReactiveHealthContributorTests.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ void asHealthContributorReturnsAdaptedContributor() {
3939
.fromMap(reactiveContributors);
4040
CompositeHealthContributor adapted = contributor.asHealthContributor();
4141
HealthIndicator byName = (HealthIndicator) adapted.getContributor("test");
42-
assertThat(byName.health(true).getDetails()).containsEntry("spring", "boot");
42+
assertThat(byName).isNotNull();
43+
Health health = byName.health(true);
44+
assertThat(health).isNotNull();
45+
assertThat(health.getDetails()).containsEntry("spring", "boot");
4346
HealthContributors.Entry entry = adapted.iterator().next();
4447
assertThat(entry.name()).isEqualTo("test");
4548
HealthIndicator byEntry = (HealthIndicator) entry.contributor();
46-
assertThat(byEntry.health(true).getDetails()).containsEntry("spring", "boot");
49+
health = byEntry.health(true);
50+
assertThat(health).isNotNull();
51+
assertThat(health.getDetails()).containsEntry("spring", "boot");
4752
}
4853

4954
@Test

module/spring-boot-health/src/test/java/org/springframework/boot/health/contributor/HealthContributorsAdapterTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void iteratorWhenDelegateContainsHealthIndicatorAdaptsDelegate() {
4242
assertThat(adapted.name()).isEqualTo("test");
4343
assertThat(adapted.contributor()).isInstanceOf(ReactiveHealthIndicator.class);
4444
Health health = ((ReactiveHealthIndicator) adapted.contributor()).health(true).block();
45+
assertThat(health).isNotNull();
4546
assertThat(health.getStatus()).isEqualTo(Status.UP);
4647
assertThat(health.getDetails()).containsEntry("spring", "boot");
4748
}
@@ -61,7 +62,9 @@ void iteratorWhenDelegateContainsCompositeHealthContributorAdaptsDelegate() {
6162
assertThat(adapted.contributor()).isInstanceOf(CompositeReactiveHealthContributor.class);
6263
ReactiveHealthContributor nested = ((CompositeReactiveHealthContributor) adapted.contributor())
6364
.getContributor("test1");
65+
assertThat(nested).isNotNull();
6466
Health health = ((ReactiveHealthIndicator) nested).health(true).block();
67+
assertThat(health).isNotNull();
6568
assertThat(health.getStatus()).isEqualTo(Status.UP);
6669
assertThat(health.getDetails()).containsEntry("spring", "boot");
6770
}
@@ -73,7 +76,9 @@ void getContributorAdaptsDelegate() {
7376
.fromMap(Collections.singletonMap("test", indicator));
7477
HealthContributorsAdapter adapter = createAdapter(delegate);
7578
ReactiveHealthContributor adapted = adapter.getContributor("test");
79+
assertThat(adapted).isNotNull();
7680
Health health = ((ReactiveHealthIndicator) adapted).health(true).block();
81+
assertThat(health).isNotNull();
7782
assertThat(health.getStatus()).isEqualTo(Status.UP);
7883
assertThat(health.getDetails()).containsEntry("spring", "boot");
7984
}

module/spring-boot-health/src/test/java/org/springframework/boot/health/contributor/HealthContributorsTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.stream.Stream;
2020

21+
import org.jspecify.annotations.Nullable;
2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.boot.health.contributor.HealthContributors.Entry;
@@ -45,7 +46,7 @@ public Stream<Entry> stream() {
4546
}
4647

4748
@Override
48-
public HealthContributor getContributor(String name) {
49+
public @Nullable HealthContributor getContributor(String name) {
4950
return null;
5051
}
5152

@@ -60,6 +61,7 @@ void createEntryWhenNameIsEmptyThrowsException() {
6061
}
6162

6263
@Test
64+
@SuppressWarnings("NullAway") // Test null check
6365
void createEntryWhenContributorIsNullThrowsException() {
6466
assertThatIllegalArgumentException().isThrownBy(() -> new Entry("test", null))
6567
.withMessage("'contributor' must not be null");

module/spring-boot-health/src/test/java/org/springframework/boot/health/contributor/HealthIndicatorTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@ class HealthIndicatorTests {
3232
@Test
3333
void getHealthWhenIncludeDetailsIsTrueReturnsHealthWithDetails() {
3434
Health health = this.indicator.health(true);
35+
assertThat(health).isNotNull();
3536
assertThat(health.getStatus()).isEqualTo(Status.UP);
3637
assertThat(health.getDetails()).containsEntry("spring", "boot");
3738
}
3839

3940
@Test
4041
void getHealthWhenIncludeDetailsIsFalseReturnsHealthWithoutDetails() {
4142
Health health = this.indicator.health(false);
43+
assertThat(health).isNotNull();
4244
assertThat(health.getStatus()).isEqualTo(Status.UP);
4345
assertThat(health.getDetails()).isEmpty();
4446
}

module/spring-boot-health/src/test/java/org/springframework/boot/health/contributor/HealthTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
class HealthTests {
3939

4040
@Test
41+
@SuppressWarnings("NullAway") // Test null check
4142
void statusMustNotBeNull() {
4243
assertThatIllegalArgumentException().isThrownBy(() -> new Health.Builder(null, null))
4344
.withMessageContaining("'status' must not be null");

module/spring-boot-health/src/test/java/org/springframework/boot/health/contributor/MapCompositeHealthContributorTests.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import java.util.function.Function;
2121
import java.util.stream.Stream;
2222

23+
import org.jspecify.annotations.Nullable;
24+
2325
import org.springframework.boot.health.contributor.HealthContributors.Entry;
2426

27+
import static org.assertj.core.api.Assertions.assertThat;
28+
2529
/**
2630
* Tests for {@link MapCompositeHealthContributor}.
2731
*
@@ -31,8 +35,9 @@ class MapCompositeHealthContributorTests
3135
extends MapCompositeTests<CompositeHealthContributor, HealthContributor, Entry> {
3236

3337
@Override
38+
@SuppressWarnings("NullAway") // Test null check
3439
protected CompositeHealthContributor create(Map<String, String> map,
35-
Function<String, HealthContributor> valueAdapter) {
40+
@Nullable Function<String, HealthContributor> valueAdapter) {
3641
return new MapCompositeHealthContributor<>(map, valueAdapter);
3742
}
3843

@@ -42,7 +47,7 @@ protected Stream<Entry> stream(CompositeHealthContributor composite) {
4247
}
4348

4449
@Override
45-
protected HealthContributor getContributor(CompositeHealthContributor composite, String name) {
50+
protected @Nullable HealthContributor getContributor(CompositeHealthContributor composite, String name) {
4651
return composite.getContributor(name);
4752
}
4853

@@ -52,8 +57,10 @@ protected HealthContributor createContributor(String data) {
5257
}
5358

5459
@Override
55-
protected String getData(HealthContributor contributor) {
56-
return (String) ((HealthIndicator) contributor).health().getDetails().get("data");
60+
protected @Nullable String getData(HealthContributor contributor) {
61+
Health health = ((HealthIndicator) contributor).health();
62+
assertThat(health).isNotNull();
63+
return (String) health.getDetails().get("data");
5764
}
5865

5966
@Override

module/spring-boot-health/src/test/java/org/springframework/boot/health/contributor/MapCompositeReactiveHealthContributorTests.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
import java.util.function.Function;
2121
import java.util.stream.Stream;
2222

23+
import org.jspecify.annotations.Nullable;
2324
import reactor.core.publisher.Mono;
2425

2526
import org.springframework.boot.health.contributor.ReactiveHealthContributors.Entry;
2627

28+
import static org.assertj.core.api.Assertions.assertThat;
29+
2730
/**
2831
* Tests for {@link MapCompositeReactiveHealthContributor}.
2932
*
@@ -33,8 +36,9 @@ class MapCompositeReactiveHealthContributorTests
3336
extends MapCompositeTests<CompositeReactiveHealthContributor, ReactiveHealthContributor, Entry> {
3437

3538
@Override
39+
@SuppressWarnings("NullAway") // Test null check
3640
protected CompositeReactiveHealthContributor create(Map<String, String> map,
37-
Function<String, ReactiveHealthContributor> valueAdapter) {
41+
@Nullable Function<String, ReactiveHealthContributor> valueAdapter) {
3842
return new MapCompositeReactiveHealthContributor<>(map, valueAdapter);
3943
}
4044

@@ -44,7 +48,8 @@ protected Stream<Entry> stream(CompositeReactiveHealthContributor composite) {
4448
}
4549

4650
@Override
47-
protected ReactiveHealthContributor getContributor(CompositeReactiveHealthContributor composite, String name) {
51+
protected @Nullable ReactiveHealthContributor getContributor(CompositeReactiveHealthContributor composite,
52+
String name) {
4853
return composite.getContributor(name);
4954
}
5055

@@ -54,8 +59,10 @@ protected ReactiveHealthContributor createContributor(String data) {
5459
}
5560

5661
@Override
57-
protected String getData(ReactiveHealthContributor contributor) {
58-
return (String) ((ReactiveHealthIndicator) contributor).health().block().getDetails().get("data");
62+
protected @Nullable String getData(ReactiveHealthContributor contributor) {
63+
Health health = ((ReactiveHealthIndicator) contributor).health().block();
64+
assertThat(health).isNotNull();
65+
return (String) health.getDetails().get("data");
5966
}
6067

6168
@Override

module/spring-boot-health/src/test/java/org/springframework/boot/health/contributor/MapCompositeTests.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.function.Function;
2525
import java.util.stream.Stream;
2626

27+
import org.jspecify.annotations.Nullable;
2728
import org.junit.jupiter.api.Test;
2829

2930
import static org.assertj.core.api.Assertions.assertThat;
@@ -41,12 +42,14 @@
4142
abstract class MapCompositeTests<T, C, E> {
4243

4344
@Test
45+
@SuppressWarnings("NullAway") // Test null check
4446
void createWhenMapIsNullThrowsException() {
4547
assertThatIllegalArgumentException().isThrownBy(() -> createWithData(null, Function.identity()))
4648
.withMessage("'map' must not be null");
4749
}
4850

4951
@Test
52+
@SuppressWarnings("NullAway") // Test null check
5053
void createWhenValueAdapterIsNullThrowsException() {
5154
assertThatIllegalArgumentException().isThrownBy(() -> createWithData(Collections.emptyMap(), null))
5255
.withMessage("'valueAdapter' must not be null");
@@ -133,19 +136,21 @@ private T createWithData(Map<String, String> map, Function<String, String> dataA
133136
return create(map, (dataAdapter != null) ? (key) -> createContributor(dataAdapter.apply(key)) : null);
134137
}
135138

136-
private String getContributorData(T composite, String name) {
137-
return getData(getContributor(composite, name));
139+
private @Nullable String getContributorData(T composite, String name) {
140+
C contributor = getContributor(composite, name);
141+
assertThat(contributor).isNotNull();
142+
return getData(contributor);
138143
}
139144

140-
protected abstract T create(Map<String, String> map, Function<String, C> valueAdapter);
145+
protected abstract T create(Map<String, String> map, @Nullable Function<String, C> valueAdapter);
141146

142147
protected abstract Stream<E> stream(T composite);
143148

144-
protected abstract C getContributor(T composite, String name);
149+
protected abstract @Nullable C getContributor(T composite, String name);
145150

146151
protected abstract C createContributor(String data);
147152

148-
protected abstract String getData(C contributor);
153+
protected abstract @Nullable String getData(C contributor);
149154

150155
protected abstract String getName(E entry);
151156

0 commit comments

Comments
 (0)